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
How to Get Time Since File Was Last Modified in Seconds with Bash
Loading Linux Text File into Excel Using Vba
Sed Help: Matching and Replacing a Literal "\N" (Not the Newline)
Setup Cron Tab to Specific Time of During Weekdays
Linux Kernel: How to Capture a Key Press and Replace It with Another Key
Linux Synchronization with Fifo Waiting Queue
How to Do Runtime Binding Based on CPU Capabilities on Linux
Nginx: [Emerg] Unknown Directive " " in /Etc/Nginx/Sites-Enabled/Example.Com:3
Tracking Threads Memory and CPU Consumption
Curl Simple File Upload - 417 Expectation Failed
Performance - Multithreaded or Multiprocess Applications
Difference Between Number in the Same Column Using Awk
How to Use Local and Remote Variables Within a Heredoc or Command Over Ssh
Create .So Files on Linux Without Using Pic (Position Independent Code) (X86 32Bit)