简体   繁体   English

按下按钮时检测 KeyDown

[英]Detecting KeyDown when pressing a button

I'm working on an AutoClicker and I want to be able turn the clicker on or off with a hotkey.我正在使用 AutoClicker,我希望能够使用热键打开或关闭答题器。 I also want the hotkey to be bindable so for starters, it'd be nice to click the button and detect what was pressed on the keyboard but whatever I try it just doesn't want to work.我还希望热键是可绑定的,因此对于初学者来说,单击按钮并检测键盘上按下的内容会很好,但是无论我尝试什么,它都不想工作。 I've set KeyPreview to true, I've tried what feels like a million ways to do this already.我已经将KeyPreview设置为 true,我已经尝试过一百万种方法来做到这一点。

private void autoClickerHKey_KeyDown(object sender, KeyEventArgs e)
        {
            this.autoClickerHKey.Text = "CLICKER BIND: " + Keys.KeyCode.ToString();
            // HKey = Keys.KeyCode.ToString();
        }

Tell me if I'm doing this wrong, but in my head, I can just set HKey to Keys.KeyCode.ToString() and then use HKey in if(keyData == Keys.HKey) to toggle my switch on or off.如果我做错了,请告诉我,但在我的脑海中,我可以将HKey设置为Keys.KeyCode.ToString() ,然后在if(keyData == Keys.HKey)使用HKey来打开或关闭我的开关。

I'm probably missing something very obvious here, but I've tinkered on this for so long I honestly can't think anymore.我可能在这里遗漏了一些非常明显的东西,但我已经修补了很长时间,老实说,我再也无法思考了。

using e.KeyCode.ToString() worked over Keys.KeyCode.ToString() .使用e.KeyCode.ToString()处理Keys.KeyCode.ToString() I think there was something wrong with my project because I'm pretty sure I tried that method before.我认为我的项目有问题,因为我很确定我以前尝试过这种方法。 Anyways, after just redoing everything as a new project, it works.无论如何,在将所有内容作为新项目重做之后,它就可以工作了。 The issue might've also been me using a bunifu button instead of the default windows button.问题也可能是我使用 bunifu 按钮而不是默认的 Windows 按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM