How to subdue the keyboard?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to subdue the keyboard?
# 1  
Old 06-08-2015
How to subdue the keyboard?

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...
# 2  
Old 06-08-2015
You could compare if the current key pressed is equal to the last key pressed, respectivly if it already has been printed.
Dunno what that stty is about, and that usage of dd as streamreader is also still new to me, but nicely made!

You're not aiming for a typewriter style are you?
# 3  
Old 06-08-2015
Hi sea...

Telegraph key - Wikipedia, the free encyclopedia
(The bug key...)

I have already written the morse practice oscillator but one has to tap keys "o" and "p" for the dits and dahs... Works perfectly in this mode at about 10WPM but I want to try and emulate the above using these two keys...

Boy is this difficult. I solved it ages ago in Python but I have no udea how to solve it in bash...

Hence asking for help...

Last edited by wisecracker; 06-08-2015 at 04:58 PM.. Reason: Pointed to the wrong paragraph in the URL.
# 4  
Old 06-08-2015
Quote:
I have already written the morse practice oscillator but one has to tap keys "o" and "p" for the dits and dahs...
Sounds awesome!

Anyway, since you are expecting 'chars', you could just run a case or if block catching the expected ones, and ignore (and/or trap if required/convenient) the other chars.

How about this?
Code:
while read -n1 CHAR
do case "$CHAR" in
    p) (sleep 0.5;echo dah)& ;;
    o) (sleep 0.5;echo dit)& ;;
    q) break ;;
    esac
done

It would 'act' on keypress, but delay the output.

hth

---------- Post updated at 22:19 ---------- Previous update was at 22:15 ----------

Doh, now i get it....
You'd need to catch an actual keypress, or... pipe that output to a function which then returns something when the stream ends.
EDIT: Like a counter value of the time when it started/ended, and then decide according to that value, wether to dit or dah.

hth

---------- Post updated at 22:24 ---------- Previous update was at 22:19 ----------

Like, writing a tempfile:0, first start if 0 add 2 and reset the $TIME_PRESSED, after which you keep adding 1 to tempfile:$tempfile.
And when the stream stops, you set it back to 0, at which time you decide for dit/dah.
This User Gave Thanks to sea For This Post:
# 5  
Old 06-08-2015
Sounds like you're having trouble with the keyboard auto repeat.

If your in an X session try xset r off
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 06-08-2015
If I understand what you're trying to do, what you want is to get the time elapsed between when a key was pressed and when that key was released so you can determine whether the operator held the key for a short time (dot) or a long time (dash). That is something that the keyboard driver would know, but I don't know of any portable interface that will make that information available to a shell script. The keyboard driver probably also ignores very short duration presses and releases (key bounce) which may be a problem if you can get raw access to keyboard events somehow in a C program or shell script.

With my current keyboard driver settings (which on OS X are settable in the keyboard section of the system preferences app; but not by the stty utility) for key press repeat and time between repeated characters, I can easily manually press and release a about 7 characters/second. And, if I hold a key down for a while, it will generate about 14 characters/second. Trying to detect whether I tried to input two dashes or two (or three) dots or a dot and a dash (in either order) using a .5 second timer isn't likely to work.

If you are using an X application and can turn off key repeat using shell commands (as Chubbier_XL suggested), you still won't know whether a key press was intended to be a dot or a dash.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 06-09-2015
@ sea...

Already tried your temp file idea. It was messy and didn't work.
The keystrokes are put into a type ahead buffer as single characters but look like a string in screen.

@ Chubler_XL...

Yup, I think you have nailed the problem and no it is not an X session...

@ Don...

You are on the right lines except because the first keyhold is extended by default then holding for one dah is not a problem, similarly for dits.
However a real "bug key" can be just held in either a dit or a dah indefinitely and as many of these are produced for the duration of that hold only.
read -s -n1 KEY does not stop the key strokes into the type ahead buffer after one character but keeps on reading whatever is in there each loop.
Even intialising every loop with KEY="?" does not prevent it...

BTW sleep 0.5 is just there as an example the real command is sox and the problem still exists...

I think you have given me my answer and it looks as though my thoughts were correct.
It can't be done in ordinary shell scripting.

I already have a good working model using single keystrokes and will upload when tidied up a bit.

Got to add random charcters, A -Z, 0 - 9, to generate the morse tones to listen to and practice aagainst.

---------- Post updated at 03:52 PM ---------- Previous update was at 07:53 AM ----------

Hi Don...

I have been experimenting with the KB timers and it is a fudge but might just do what I want.

Not really the way to go but an alternative nevertheless...

The morse practice oscillator will be on here in the not too distant future...

Thanks all...
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem getting vertical bar with British keyboard layout on US (physical) keyboard

Hi, I've got a bit of a ridiculous problem and wasn't sure where to post it. I need to use the vertical bar for piping in Bash but, as per the title, am using a UK layout on a US (physical) keyboard which doesn't have a key for it in the place I'd expect. I've tried using xbindkeys and Unicode... (7 Replies)
Discussion started by: crunchgargoyle
7 Replies

2. What is on Your Mind?

Keyboard vs mouse

Which Input device do you use the most ? for me... keyboard ofcourse !! (56 Replies)
Discussion started by: vpraveen84
56 Replies

3. Solaris

Keyboard problem

Hi to everyone I am new at Solaris world so I need little help. I can not put my keyboard to Croatian layout. I tried to edit /etc/default/init and in that file I changed LC_ALL=hr_HR. Also I tried to change my language settings over GUI (input methods and SCIIM) but with no results. Also tried... (11 Replies)
Discussion started by: microbot
11 Replies

4. UNIX for Dummies Questions & Answers

Keyboard keeps locking up

First off, let me start by saying that I am a total rookie when it comes to Unix so I will do my best to explain the situation. BACKGROUND:We are running AIX and using a third party Inventory Management software called Acclaim. My main interface terminal is just a "dummy" terminal hooked up to... (1 Reply)
Discussion started by: sstaszak11
1 Replies

5. Linux

mdk 10.2 and keyboard

Hi all, I'm running a mdk 10.2 cooker on my computer. After and update, i've lost the "alt gr" key. How am i supposed to get it back? I've already googled around a bit, testing solutions provided, but nothing can help.. any other suggestions? Thanx all Jason (3 Replies)
Discussion started by: penguin-friend
3 Replies

6. SCO

Kill from keyboard

I have a cron job that creates my backup tape. However my commands in this job are timing out, therefore "Retrying job retry" is the loop it is hung into. I cannot telnet to the box nor get a login prompt. What is the kill command keystone stroke sequence that will break a cron job? Thanks, (3 Replies)
Discussion started by: jwideman
3 Replies

7. UNIX for Advanced & Expert Users

How can I map Unix keyboard for PC keyboard

A Solaris AXI 440 machine with Solaris 8 version. I have PC users who use an emulation to login to the Solaris server. How can I change the keyboard mapping of the Sun keyboard to fit to the PC keyboard ? Any comment will be appreciated. Thanks (1 Reply)
Discussion started by: simhab
1 Replies
Login or Register to Ask a Question