welford
Posts: 20
Joined: Fri Jun 29, 2012 12:05 am

keyboard input linux/input.h

Sat Aug 04, 2012 1:39 pm

Hello,

I'm working on a simple C program to grab input from a keyboard via /dev/input/event(N) which seems to be working fine, but the first input_event I get for a single key press has values .type = 4 (EV_MSC) .code = 4 (MSC_SCAN? i think this is what it corresponds to) and then a .value of 458782 for '1' or 458756 for 'a' . The .value seems to increase according to which key is pressed, though not exactly as one would expect.

My question is what the the significance of MSC_SCAN, and the value? and if someone could point me to a explanation of these that would be great.

Thanks for any help

Janq
Posts: 36
Joined: Sat Jun 02, 2012 3:36 pm

Re: keyboard input linux/input.h

Sat Aug 04, 2012 2:55 pm

The key scancodes are defined in linux/input.h. Look for KEY_*. e.g. a is KEY_A.

welford
Posts: 20
Joined: Fri Jun 29, 2012 12:05 am

Re: keyboard input linux/input.h

Sat Aug 04, 2012 11:15 pm

Thanks for the reply Janq, but I am already able to get the key pressed. My question was about the first event that I get. For example if I press 'a' I get 3 input_events with the contents being

input_event0{type=4, code=4, value=458796} <- what is this?
input_event1{type=1, code=30, value=1} <- this is the key press for 'a'
input_event2{type=0, code=0, value=0}

My question was what is the significance of input_event0?
a little googling tells me that type 4 is EV_MSC and code 4 is MSC_SCAN? but what does the value represent?

Daverj
Posts: 28
Joined: Tue Mar 06, 2012 2:23 am
Contact: Website

Re: keyboard input linux/input.h

Sun Aug 05, 2012 1:01 am

My understanding is that EV_MSC / MSC_SCAN is used for remapping keys. So a wild guess is that the value represents a physical key on the input device (keyboard, etc...) and could be used to recognize that specific physical button as being pressed.

Where as the EV_KEY / KEY_xxx is the way the system has mapped that specific physical button. For example the "#" character is mapped to different physical buttons based on the country the keyboard is made for.

welford
Posts: 20
Joined: Fri Jun 29, 2012 12:05 am

Re: keyboard input linux/input.h

Sun Aug 05, 2012 2:33 am

Thank you Daverj.

Is there any documentation of the remapping process available online?

Janq
Posts: 36
Joined: Sat Jun 02, 2012 3:36 pm

Re: keyboard input linux/input.h

Sun Aug 05, 2012 4:54 am

Ah sorry - I see what you mean now. imo you should use EV_KEY instead - EV_MSC/MSC_SCAN returns a device specific scan code afaik - probably not very useful. EV_KEY returns the KEY_* enum in input_event.code which is guaranteed to be the same for every device. You can actually read joysticks/gamepads this way too but I recommend using the JSIO interface for that - its much simpler. (If you use udev to find out where all of the keyboards/mice/controllers map to in /dev that will give you the JSIO devices anyway).

Daverj
Posts: 28
Joined: Tue Mar 06, 2012 2:23 am
Contact: Website

Re: keyboard input linux/input.h

Sun Aug 05, 2012 9:49 pm

welford wrote:Thank you Daverj.

Is there any documentation of the remapping process available online?
I haven't seen it. I assume it's buried in the kernel config files for keyboards.

When you first run the Pi, or run raspi-config, and choose to configure the keyboard the first step is to select the type of keyboard. The next step is to select the country/language map for the keyboard. My guess is that the first choice is choosing the hardware, which in turn is determining the scancodes for the physical buttons. The second choice is choosing the remapping of the keyboard to assign the keys to have the meaning of what is printed on the keys themselves. (ie: the same physical keyboard might be manufactured with different keys marked in different ways) By choosing the country/language for your keyboard you are determining which arrangement of those keys your keyboard has.

Personally if you are looking for keys that are hit by the user, I would ignore the EV_MSC/MSC_SCAN and just use the EV_KEY keycode values. Not only is it telling you what key the person typed (letter, number, arrows, etc...) but it is also telling you if they pressed it, released it, or if it is auto repeating. Those aren't conveyed by the scancode in the MSC_SCAN value.

If you used the MSC_SCAN scancodes you would be bypassing the keyboard mapping that the user selected when they configured their Pi and would not see the correct keys in many cases. The whole point of having that abstraction is so your program doesn't have to know about hundreds of types of keyboards and a thousand different keyboard country/language mappings. You just need to know that the person pressed the "#" key (or whatever key), no matter where it is on their specific keyboard.

welford
Posts: 20
Joined: Fri Jun 29, 2012 12:05 am

Re: keyboard input linux/input.h

Mon Aug 06, 2012 2:09 pm

thanks again Daverj and Janq :)

Return to “C/C++”