更新时间:2023-12-03 09:46:16
我发现的唯一方法是结合使用调用 GetKeyState
API 函数 (user32.dll) 和 定时器
.以下是它在测试应用上的运行情况:
The only way I found was to use a combination of calling the GetKeyState
API function (user32.dll) and of a Timer
. Here is as it works on the test app:
System.Windows.Forms.Timer keyManagerTimer = new System.Windows.Forms.Timer();
int count = 0;
public Form1()
{
InitializeComponent();
this.keyManagerTimer.Tick += (s, e) => ProcessKeys();
this.keyManagerTimer.Interval = 25;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData & Keys.Right) != 0)
{
keyManagerTimer.Enabled = true;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void ProcessKeys()
{
bool isShiftKeyPressed = IsKeyPressed(Keys.ShiftKey);
bool isRightKeyPressed = IsKeyPressed(Keys.Right);
if (isRightKeyPressed && !isShiftKeyPressed)
{
count++;
}
else if (isRightKeyPressed && isShiftKeyPressed)
{
count += 10;
}
label.Text = "count = " + count.ToString();
}
public static bool IsKeyPressed(Keys key)
{
return BitConverter.GetBytes(GetKeyState((int)key))[1] > 0;
}
[DllImport("user32")]
private static extern short GetKeyState(int vKey);
在我的实际代码中,我在拥有视频的 Control
的 Leave
事件上禁用了 Timer
.另一种解决方案可能是使用 IMessageFilter(请参阅此处).
In my real code then I disable the Timer
on the Leave
event of the Control
where I have the video. Possibly another solution might have been to use the IMessageFilter (see here).