이것은 내가 이해하지 못하는 꽤 기본적인 개념 인 것 같습니다. 키보드 드라이버에 대한 .NET 래퍼를 작성에서이벤트 핸들러에 전달 된 구조체를 수정 하시겠습니까?
, 난 그렇게 (간체 코드 아래)처럼 누르면 각 키에 대한 이벤트를 방송하고 있습니다 :
// The event handler applications can subscribe to on each key press
public event EventHandler<KeyPressedEventArgs> OnKeyPressed;
// I believe this is the only instance that exists, and we just keep passing this around
Stroke stroke = new Stroke();
private void DriverCallback(ref Stroke stroke...)
{
if (OnKeyPressed != null)
{
// Give the subscriber a chance to process/modify the keystroke
OnKeyPressed(this, new KeyPressedEventArgs(ref stroke));
}
// Forward the keystroke to the OS
InterceptionDriver.Send(context, device, ref stroke, 1);
}
스트로크가의 스캔 코드를 포함하는 struct
입니다 눌려진 키 및 상태를 표시합니다.
위 코드에서 value-type 구조체를 참조로 전달하기 때문에 구조체에 대한 변경 사항은 OS에 전달 될 때 '기억'됩니다 (눌려진 키가 가로 채고 수정 될 수 있음). 그래서 괜찮습니다.
그러나 내 OnKeyPressed
이벤트 구독자가 struct
Stroke
을 수정하도록 허용하려면 어떻게해야합니까?
다음은 작동하지 않습니다
public class KeyPressedEventArgs : EventArgs
{
// I thought making it a nullable type might also make it a reference type..?
public Stroke? stroke;
public KeyPressedEventArgs(ref Stroke stroke)
{
this.stroke = stroke;
}
}
// Other application modifying the keystroke
void interceptor_OnKeyPressed(object sender, KeyPressedEventArgs e)
{
if (e.stroke.Value.Key.Code == 0x3f) // if pressed key is F5
{
// Doesn't really modify the struct I want because it's a value-type copy?
e.stroke.Value.Key.Code = 0x3c; // change the key to F2
}
}
사전에 감사합니다.
if (OnKeyPressed != null)
{
// Give the subscriber a chance to process/modify the keystroke
var args = new KeyPressedEventArgs(stroke);
OnKeyPressed(this, args);
stroke = args.Stroke;
}
이 당신의 가입자 사본을주고 그들이 그것으로 완료되면 다시 해당 지역의 값으로 복사 :이 같은
'스트로크'를 nullable로 설정하면 값이 있는지 여부를 나타내는 부울을 포함하는 래퍼에 실제 값이 저장됩니다. 래핑 된 값은 하나부터 시작된 값 유형으로 남아 있습니다. –
@ EricJ.의 덧붙여 말하면 nullable 형식은 컴파일러에서 많은 특수 처리를하는 값 형식이지만 고유 한 값 형식이기도합니다. – phoog