我的程序将“Z”键绑定到激活计时器的处理程序。 该计时器触发鼠标点击。
问题是,如果我按住Z超过5秒,它就会卡住而在KeyUp上它不会触发,变量不会变为false
并且循环是无限的,所以它继续不再按下按键时触发计时器的回调。只有通过ALT+F4
我的代码位于http://pastebin.com/rbCgY1rb
我使用globalKeyboardHook from here
代码的关键部分是:
private void keyDownCallback(object sender, KeyEventArgs e) {
if (e.KeyCode.ToString() == "Z") {
timer1.Enabled = true;
this.forceNoLoop = false;
} else if(e.KeyCode.ToString() == "X") {
timer1.Enabled = false;
this.forceNoLoop = true;
}
}
private void keyUpCallback(object sender, KeyEventArgs e) {
timer1.Enabled = false;
this.forceNoLoop = true;
}
private void timer1_Tick(object sender, EventArgs e) {
if (forceNoLoop) return;
mouse_event(MOUSEEVENTF_LEFTDOWN, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
lblClickStatus.Text = "clicked " + (this.clickTimes++) + " times";
}
所以问题是:如何解决冻结问题?
答案 0 :(得分:1)
您可以在启用/禁用它之前尝试检查计时器的状态吗?
private void keyDownCallback(object sender, KeyEventArgs e) {
if (e.KeyCode.ToString() == "Z") {
if (!timer1.Enabled)
timer1.Enabled = true;
} else if (e.KeyCode.ToString() == "X") {
if (timer1.Enabled)
timer1.Enabled = false;
}
}