[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: How to detect a keypress, and in which language?



On Sun, Jan 20, 2008 at 04:27:50PM -0600, Hugo Vanwoerkom wrote:
> Wayne Topa wrote:
>> Kent West(westk@acu.edu) is reported to have said:
>>> Hugo Vanwoerkom wrote:
>>>> Kent West wrote:
>>>>> All I want to do is to detect two keys, say the left- and 
>>>>> right-shift keys, or the < and > keys. For one key, a short "dit" 
>>>>> audio tone would be generated, and for the other key, a longer 
>>>>> "dah" audio tone would be generated. I need to bypass the 
>>>>> keyboard buffer, so that holding down the dit key for two seconds 
>>>>> doesn't generate 30 dits; it should produce dits while the key is 
>>>>> held down, but once the key is let up, the dits should  
>>>>> immediately stop (after finishing the one it's on).
>>>>>
...
>> This may not be what your looking for either Kent, but...
>>
>> Install the beep package.  Then, using bash, write a program to output
>> to the case speaker like this.
>>
>> A="/usr/bin/beep -l 80 -f 1000 ;sleep .1;/usr/bin/beep -d 100 -l 250  -f 1000"
>> B="/usr/bin/beep -d 100 -l 250 -f 1000 ; sleep .1;/usr/bin/beep -l 80 -r 3 -f 1000"
>> etc
>
> Turns out that keypress access *is* the hardest. You have that in Qt but  
> you have to be running in X and within a widget. It's ridiculous to do  
> all that to find out if a key is pressed.
>
> I have no idea how to find out in low-level programming whether a key is  
> depressed.
>
> Any kernel savvy people out there?

That's not me, but I'm sure there are many ways to do this in higher
level languages.  Here's a bit o' perl that loops, blocks until a key
is pressed, does something with the key and so on.  I've used the beep
package mentioned above, and that could likely be used to make the sounds.

    #!/usr/bin/perl -w
    use strict;
    use Term::ReadKey;
    ReadMode 4; # put terminal in raw mode
    while ( 1 ) {
        my $key = ReadKey 0; # no time-out, blocks until a key is pressed
        last if $key eq 'q';
        print "dot\n" if $key eq 'j';
        print "dash\n" if $key eq 'k';
        }
    ReadMode 0; # restore terminal mode

Perl generally provides a wrapper around kernel and system level routines,
and I'm sure Python and other languages have these same capabiities.

Looking at the OP's description/spec of what's to be done, the above
doesn't do it, but could probably be made to approach it.  The ReadKey
function can be given a timeout, so that if no key is pressed it returns
with no return value, and this could be used to detect if a key is held
down (I think...).

Emitting the sounds while this is going on might be a trick.  One way
would be to fork another process to make the sounds, and communicate
with it via a pipe to turns the dits and dahs on and off.  This may 
sound complicated, but it's the Unix Way, and the tools support it 
very well.

Just a thought...

-- 
Ken Irving, fnkci+debianuser@uaf.edu


Reply to: