I'm developing an application in which I need to read the input of a trigger on a webcam to tell my application to do something.

The project involves OpenCV built with QT support, and I will randomly get crashes when I apply this trigger (ASSERT: "false" in file qasciikey.cpp, line 495).

So I figured an easier and more robust way is to map the external trigger to a key (spacebar, specifically). I don't want to override the spacebar, just to have the trigger look like the spacebar was pressed.

So far, using evtest, I select my device:

/dev/input/event13: See3CAM_CU51

And can see when I trigger the camera, it displays the following:

Event: time 1507757166.472300, type 1 (EV_KEY), code 212 (KEY_CAMERA), value 1
Event: time 1507757166.472300, -------------- SYN_REPORT ------------
Event: time 1507757167.147649, type 1 (EV_KEY), code 212 (KEY_CAMERA), value 0
Event: time 1507757167.147649, -------------- SYN_REPORT ------------

The "value 1" is when the pins are pulled high (triggered), and "value 0" are when the pins are separated. So I think I only need to remap KEY_CAMERA to the spacebar, but I'm not sure the cleanest way to do it using Ubuntu 16.04.

When I select my keyboard device:

/dev/input/event4: AT Translated Set 2 keyboard

And I press spacebar, I get the following output:

Event: time 1507757327.011812, -------------- SYN_REPORT ------------
Event: time 1507757328.818177, type 4 (EV_MSC), code 4 (MSC_SCAN), value 39
Event: time 1507757328.818177, type 1 (EV_KEY), code 57 (KEY_SPACE), value 1
Event: time 1507757328.818177, -------------- SYN_REPORT ------------
Event: time 1507757328.896970, type 4 (EV_MSC), code 4 (MSC_SCAN), value 39
Event: time 1507757328.896970, type 1 (EV_KEY), code 57 (KEY_SPACE), value 0

I've been reading about writing the hwdb udev file, but I've yet to see a way to map an input from one device all the way to another. Is this possible? What's the easiest way of getting this done?

Thanks in advance.

  • Of course your application could catch the camera event and issue a key event, but what makes you think the behaviour would change when using a different event? What is that assertion for? – Philippos Oct 12 '17 at 6:43
  • My application is using OpenCV built with QT. QT is detecting key events (I assume). In my program I can catch a spacebar key event fine. But when I trigger the camera, the program sometimes fails, sometimes succeeds, sometimes only works once, etc. I'm blaming this one on QT. This is where it fails: github.com/openwebos/qt/blob/… – justynnuff Oct 12 '17 at 22:07

Probably the right answer is to write an [hwdb] file to override the interpretation of the keycode KEY_CAMERA into KEY_SPACE. This seems to be an example, but it is a little lacking in details.

The format of the file is mostly described in a comment at the head of the file /usr/lib/udev/hwdb.d/60-keyboard.hwdb (on my system), and you can see some archlinux discussion of that. Beware, this format depends on the release of systemd, so check your own file. You can match your input device with one of 3 patterns:

  • Generic input devices match:


    This matches on the kernel modalias of the input-device, mainly: ZZZZ is the bus-id (see /usr/include/linux/input.h BUS_*), YYYY, XXXX and WWW are the 4-digit hex uppercase vendor, product and version ID and VVVV is an arbitrary length input-modalias describing the device capabilities.

  • AT keyboard DMI data matches:


    vendor and product are the firmware-provided strings exported by the kernel DMI modalias.

  • Input driver device name and DMI data match:

      evdev:name:input device name:dmi:bvn*:bvr*:bd*:svnvendor:pn*

    input device name is the name device specified by the driver, vendor is the firmware-provided string exported by the kernel DMI modalias.

Another possibility is to write a small program to read the webcam event stream, like evtest does, and when the key is seen, inject a KEY_SPACE into the other event stream. There seems to a lot more documentation on this, and many Python examples like this tutorial snippet to inject an event.

  • I don't get the syntax of the top of the hwdb files. Ie, evdev:atkbd:dmi:bvn*:bvr*:bd*:svnAcer*:pn123*. Where can I find that for my keyboard and camera? Do you know? – justynnuff Oct 12 '17 at 22:06
  • @justynnuff I added the only information I have on the format. – meuh Oct 13 '17 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.