Jeremy
Jeremy

Reputation: 46340

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?

Upvotes: 1

Views: 2706

Answers (3)

Martin Plante
Martin Plante

Reputation: 4703

And if your application is NOT the main window, take a look at the RegisterHotkey Win32 API, with some info on p/invoke here.

Upvotes: 0

Tom Anderson
Tom Anderson

Reputation: 10827

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);
}

Upvotes: 2

Joel B Fant
Joel B Fant

Reputation: 24756

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.

Upvotes: 1

Related Questions