How to use laptop keyboard as input

Hi there,

I’m interested in prototyping a protocol on humans before running it with animals and being able to run it on a laptop where the subject can respond with the keyboard would be very helpful. I read something about a “USB HID” device but I’m not exactly sure how to set this up.

Thank you!

Javier

Hi Javier,

Yes, a USB HID device is what you need. I’ve attached a simple example experiment that reports the state of the spacebar.

The only trick with USB HID devices is figuring out the correct usage page and usage values for the input you want to monitor. With a keyboard, the easiest way to do it is to set the device’s log_all_input_values parameter to YES, press and release the desired key, and then check the output in MWServer’s console window. In my tests, every key press or release generates four HID input values. You want the usage-page/usage pair whose value goes to 1 when the key is pressed and changes to 0 when it’s released.

For example, pressing and releasing the “A” key generates the following output in the server console:

00:09:21:  HID input on device "keyboard":
    Usage page: 7   (0x07)
    Usage:      4294967295  (0xFFFFFFFF)
    Value:      4
00:09:21:  HID input on device "keyboard":
    Usage page: 7   (0x07)
    Usage:      1   (0x01)
    Value:      0
00:09:21:  HID input on device "keyboard":
    Usage page: 7   (0x07)
    Usage:      4   (0x04)
    Value:      1
00:09:21:  HID input on device "keyboard":
    Usage page: 7   (0x07)
    Usage:      4294967295  (0xFFFFFFFF)
    Value:      4
00:09:22:  HID input on device "keyboard":
    Usage page: 7   (0x07)
    Usage:      4294967295  (0xFFFFFFFF)
    Value:      0
00:09:22:  HID input on device "keyboard":
    Usage page: 7   (0x07)
    Usage:      1   (0x01)
    Value:      0
00:09:22:  HID input on device "keyboard":
    Usage page: 7   (0x07)
    Usage:      4   (0x04)
    Value:      0
00:09:22:  HID input on device "keyboard":
    Usage page: 7   (0x07)
    Usage:      4294967295  (0xFFFFFFFF)
    Value:      0

In this case, usage page 7, usage 4 has a value that’s first 1 and then 0, so that’s the pair you need.

If you need further assistance, please let me know.

Chris Stawarz

Attachment: keyboard_example.xml (2.42 KB)

Hey Chris,

Thanks for the reply. I will look into this and get back to you!

Best,

Javier