Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

How to subdue the keyboard?

$
0
0
This has got me stumped and no solution on the WWW or here either that I can see.

I suspect this can't be done... ;o(

My requirement is to hold a _random_ key down and run a single command.
Easy EXCEPT the keyboard keeps adding that same keystroke into a type ahead buffer somewhere and does NOT wait until the command has finished.

In this real example sleep is the command but almost any command does the same.
You will notice I have tried various ideas but all give the same results...

Take this real example, hold the s key down for 1 to 2 seconds and watch the results:-
Code:

#!/bin/bash
# exec 3<&0
while true
do
        KEY="?"
        stty -echo -icanon min 0 time 0
        KEY=$(dd bs=1 count=1 2> /dev/null)
        # Change -u[x] to the correct file descriptor.
        # read -s -n1 -u0 KEY
        KEY="${KEY:0:1}"
        if [ "$KEY" = "s" ]
        then
                sleep 0.5
                # wait
                printf "$KEY "
        fi
        if [ "$KEY" = "q" ]
        then
                break
        fi
done
stty sane
# exec 0<&3 3<&-
echo ""

Is it possible to just get one, and one only, ketstroke with the key depressed until the next loop just like INKEY$ in BASIC where each statement is executed sequentially?
View these incorrect results:-
Code:

Last login: Mon Jun  8 18:44:11 on ttys000
AMIGA:barrywalker~> cd ~/Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./kb
s s s s s s s s s s s s s
AMIGA:barrywalker~/Desktop/Code/Shell> _

You will notice that sleep does its job of delaying the printout but it is printing a string of "s's" instead of one just "s" then waiting before accessing the next single character.

I hope this is lucid enough.

TIA.

Bazza...

Viewing all articles
Browse latest Browse all 16232

Trending Articles