How to Translate Linux Keycodes from /Dev/Input/Event* to Ascii in Perl

How can I translate Linux keycodes from /dev/input/event* to ASCII in Perl?

It's basically a map problem. You have to take a keycode and lookup its ASCII equivalent. What about the "array part" do you think is not a good practice?

I didn't see a module for this on CPAN, but that means that you have a chance to be the first to upload it. :)

Convert ascii to input_event keycode

It has been suggested on multiple occasions that this might be a duplicate of this question: How can I translate Linux keycodes from /dev/input/event* to ASCII in Perl?. This answers the question of converting from input_event keycodes to ascii, not from ascii to keycode. That being said It looks like that code can be reverse engineered to work for my case as well.

How to bind Caps Lock key event in Perl Tk?


    use strict;
use warnings;
use Tk;


my $mw = MainWindow->new;
my $label = $mw->Label();
$mw->geometry("200x200+50+50");

$mw->bind('<Caps_Lock>' => \&exit);
MainLoop;

UPDATE: You can check the keycodes and state:

    $mw->bind('<Caps_Lock>' =>  sub { capslock($mw)});

and define a function of that name:

    sub capslock
{
my ($mw) = shift;
printf("keycode %d\n", $Tk::event->k);
printf("state %d\n", $Tk::event->s);
}

On Linux: The keycode is 66 and the state is 0 or 2 depending on whether set or not. For Windows: See my comment below.

UPDATE2: You can't complain about the service :) -

this will print the keycode and state on the label instead:

use strict;
use warnings;
use Tk;

my $mw = MainWindow->new;
my $codes = '';
my $label = $mw->Label(-font => ['courier', '12'],
-justify => 'left',-textvariable => \$codes)->pack;
$mw->geometry("200x200+50+50");
$mw->bind('<Caps_Lock>' => sub { capslock($mw)});
MainLoop;

sub capslock
{
my ($mw) = @_;

$codes = sprintf("keycode %d\nstate %d",
$Tk::event->k, $Tk::event->s);
}

Python: get keyboard input from MagStripe reader

On Linux, USB keyboards can be accessed via /dev/input.

See: format of /dev/input/event*? - the answers to that question include two different Python modules for getting events, and links to more documentation.

To translate from key codes to ASCII, see How can I translate Linux keycodes from /dev/input/event* to ASCII in Perl?

You will probably need to be root, or change the permissions on /dev/input.



Related Topics



Leave a reply



Submit