简体   繁体   中英

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. 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.

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.

I came across this article on CodeProject for handling raw input of devices.

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. We don't know.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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