简体   繁体   English

捕获按键消息

[英]Capturing key press messages

We're building some software for an in-house Kiosk. 我们正在为内部信息亭构建一些软件。 The software is a basic .net windows form with an embedded browser. 该软件是带有嵌入式浏览器的基本.net Windows窗体。 The Kiosk is outfitted with a mat that the user steps on. 信息亭配有用户可以踩踏的垫子。 When the user steps on the mat, it sends a key comination through the keyboard. 当用户踩踏垫子时,垫子会通过键盘发送按键。 When the user steps off the mat it sends a different key combination. 当用户离开垫子时,它将发送不同的组合键。

What we want to do is look for the key combination in our app, and based on if the user steps on or off, cause the browser to go to a different url. 我们要做的是在我们的应用程序中查找按键组合,并根据用户打开还是关闭,使浏览器转到其他网址。

How do you hook the keyboard to accomodate this type of situation? 您如何挂钩键盘以适应这种情况?

If your window is the active window, then you can simply override the forms ProcessCmdKey as such below. 如果您的窗口是活动窗口,则可以简单地覆盖如下所示的ProcessCmdKey形式。

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    const int WM_KEYDOWN = 0x100;
    const int WM_SYSKEYDOWN = 0x104;

    if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
    {
        switch (keyData)
        {
            case Keys.Down:
                this.Text = "Down Arrow Captured";
                break;

            case Keys.Up:
                this.Text = "Up Arrow Captured";
                break;

            case Keys.Tab:
                this.Text = "Tab Key Captured";
                break;

            case Keys.Control | Keys.M:
                this.Text = "<CTRL> + M Captured";
                break;

            case Keys.Alt | Keys.Z:
                this.Text = "<ALT> + Z Captured";
                break;
        }
    }

    return base.ProcessCmdKey(ref msg, keyData);
}

I was doing a somewhat similar search just a short while ago regarding USB card readers. 不久前,我就USB读卡器进行了类似的搜索。

I came across this article on CodeProject for handling raw input of devices. 在CodeProject上遇到了有关处理设备原始输入的文章

My main goal was differentiating multiple devices that act as keyboards. 我的主要目标是区分充当键盘的多种设备。 Your device may have a different interface. 您的设备可能具有不同的界面。 It also may have an SDK and documentation. 它还可能具有SDK和文档。 We don't know. 我们不知道

如果您的应用程序不是主窗口,请查看RegisterHotkey Win32 API, 此处有一些有关p / invoke的信息

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

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