如何使用键盘上的快捷键单击标签? C#.NET [英] How do I click a label with a shortcut key on my keyboard? C# .NET

查看:114
本文介绍了如何使用键盘上的快捷键单击标签? C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绑定键盘按钮(最好是ESC)以阻止我的软件运行,使其处于空闲模式。但问题是,它只适用于实际的按钮,无论如何都可以绕过它,所以当按下标签时它也可以工作吗?







I am trying to bind a keyboard button (preferably "ESC") to stop my software from running, putting it in a idle mode. But the thing is, it only works with actual buttons, is there anyway to bypass this so it works when pressing labels aswell?



private void script1_Click(object sender, EventArgs e)
{

            if (e.Control && e.KeyCode.ToString() == "ESC")
            {
                MessageBox.Show("This does not work");
            }
            
    launchLabel.Text = ("Script Started");

   WindowMover();

   AutoItX3 autoit = new AutoItX3();

    autoit.MouseClick("left", 468, 286, 1, 8);

    return;

}





我曾在某处读过可能只是Visual Studio GUi不提供它,但你可以用代码以某种方式做到这一点,这是真的吗?



我尝试过:



我尝试过使用按钮和标签,看起来像visual-studio不允许它。



Ive read somewhere that it is possible its just that the Visual Studio GUi doesnt provide it, but you can do it with code somehow, is this true?

What I have tried:

I've tried using a button and a label and it seems like visual-studio wont allow it.

推荐答案

没有标签,没有 - 他们不接受键盘输入所以没有为他们设置快速键设置,尽管您可以通过标签文本中的&将快捷键附加到下一个控件:& Hello会例如,在ALT + H上激活跟随控制。但这对ESC不起作用。

它可以完成 - 但具体取决于你工作的环境。由于你指定C#,我们可以排除网站(因为它必须是一个javascript的解决方案,它可能是浏览器依赖的。)

对于WinForms(可能是WPF,虽然我还没有测试过),你会覆盖Form ProcessCMdKey:

Not with labels, no - they don't accept keyboard input so there isn;t a "quick key" setting for them although you can "attach" a quick key to the next control via an '&' in the label text: "&Hello" would make the follow control active on ALT+H for example. That won't work for ESC though.
It can be done - but exactly how depends on the environment you are working in. Since you specify C#, we can exclude websites (because it would have to be a javascript solution for that, and it would likely be browser dependant).
For a WinForms (and probably WPF, though I haven't tested it) you would override the Form ProcessCMdKey:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    if (msg.WParam == (IntPtr) 0x1b)
        Console.WriteLine("Esc");

但是我使用的是 const 值,而不是神奇的数字。



看起来很可怕,看到我从未见过之前的代码让我不理解它,我确定它有效,但你能快速完成它吗?





真的不是那么可怕 - 你所做的就是重写Form程序,这样你才能得到在表单之前的消息内部(因为它会将其删除并将其指向文本框等等)。

问题是你正在尝试做一些不适合的事情进入正常的方式,所以你必须在一定程度上跳出框以使其工作!

复制下面的代码并将其粘贴到你的应用程序中,然后在调试器中运行它。查看输出窗格并输入一些东西 - 什么也不会发生。但是按ESC键你应该收到一条消息。你可以做你需要的东西而不是你的真实应用程序中的消息。

But I'd use a const value instead of the magic number.

"That looks scary, see I've never seen that code before which makes me not understand any of it, Im sure it works but could you go through it quicky? "


It's not that scary really - all you are doing is overriding a Form procedure so that you can "get at" the message internals before the form does (because it will strip it out and direct it to textboxes and so forth).
The problem is that you're trying to do something that doesn't fit into the "normal" way of things, so you have to go "outside the box" to a certain extent in order to get it to work!
Copy the code below and paste it into your app, and then run it in the debugger. Look at the Output Pane and type something - nothing will happen. But press ESC and you should get a message. Instead of a message in your "real" app, you can do what you need to.

private const int WM_KEYPRESS= 0x100;
private IntPtr ESCAPE = (IntPtr)0x1b;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    if (msg.Msg == WM_KEYPRESS && msg.WParam == ESCAPE)
        {
        Console.WriteLine("Escape pressed!");
        return true;
        }
    return base.ProcessCmdKey(ref msg, keyData);
    }

它只是查看窗口传递的消息,并截取一条特定消息(按下一键)和一个特定的键:ESC

它返回 true 让系统知道它处理它并且它不需要再传递它。

All it does is look at the messages windows is passing around, and intercept one specific message ("A key was pressed") and a specific key: ESC
It returns true to let the system know it dealt with it and it doesn't need to pass it any further.


这篇关于如何使用键盘上的快捷键单击标签? C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆