Bash INKEY$ function... | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Bash INKEY$ function...

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-25-2013
wisecracker's Avatar
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 23 October 2014, 3:18 PM EDT
Location: Loughborough
Posts: 642
Thanks: 133
Thanked 136 Times in 113 Posts
Bash INKEY$ function...

This is probably common knowledge to the professionals but not so much for amateurs like
myself.

This is a code snippet for the equivalent of BASIC's...

LET char$=INKEY$

As the timeout parameter cannot be less than 1 second then this is the only limitation...

It is a single line function which has a variable "char"...

Read the code for more information...

Issued as Public Domain...


Code:
#!/bin/bash

# An INKEY$ function for bash!
inkey() { char="" ; read -p "" -n1 -s -t1 char ; }
# Similar to BASIC's LET char$=INKEY$

# Do you remember INKEY$ in BASIC programming?
# Example:-
#
# PRINT "Some prompt:- "
# some_label:
# LET char$=INKEY$
# IF char$="<some_character>" THEN <do_something>
# IF char$="" THEN <do_something_else>
# GOTO some_label

# This is just a test piece only...
while true
do
	printf "Some prompt:- "
	# This is LET char$=INKEY$...
	inkey
	printf "Key pressed:- '$char'...\n"
	if [ "$char" == "q" ]
	then
		printf "Quitting... \n"
		break
	fi
	if [ "$char" == "" ]
	then
		printf "Timeout works OK...\n"
	fi
	if [ "$char" == "b" ]
	then
		printf "Barry Walker...\n"
	fi
done

Sponsored Links
    #2  
Old 03-25-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 October 2014, 4:38 PM EDT
Location: Saskatchewan
Posts: 19,683
Thanks: 823
Thanked 3,352 Times in 3,139 Posts
What read -n 1 is doing here is probably something like stty -icanon min 1 ; get_character ; reset

...since it's not BASH that's preventing you from reading one at a time, but the terminal itself. Usually it's in "canon" mode, or line-by-line.
Sponsored Links
    #3  
Old 03-26-2013
wisecracker's Avatar
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 23 October 2014, 3:18 PM EDT
Location: Loughborough
Posts: 642
Thanks: 133
Thanked 136 Times in 113 Posts
Hi Corona688...

Thanks for the info...
One day I will become a coder... ;o)

I have used a derivative of it in the next stage of the AudioScope as a pseudo vertical shift control, so I thought I would let other people like me have it...
    #4  
Old 03-26-2013
rbatte1 rbatte1 is offline Forum Staff  
Moderator
 
Join Date: Jun 2007
Last Activity: 24 October 2014, 3:13 PM EDT
Location: Lancashire, UK
Posts: 1,869
Thanks: 599
Thanked 322 Times in 290 Posts
What's wrong with

Code:
stty raw -echo
inkey=`dd bs=1 count=1 2>/dev/null`
stty -raw echo

...that someone excellently posted a while back. This can be tweaked for particular shell, but doesn't rely on the shell's inbuilt functions and is less susceptible to the variations across OS flavours.


Bring back the ZX-Spectrum (Ran out of colours!)


Robin
Sponsored Links
    #5  
Old 03-26-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 October 2014, 4:38 PM EDT
Location: Saskatchewan
Posts: 19,683
Thanks: 823
Thanked 3,352 Times in 3,139 Posts
For one thing, it assumes that a keystroke is one character(frequently untrue).

For another, it runs stty twice per keystroke... I'd be tempted to leave the terminal as is instead of changing it that often, save you a lot of CPU time.

This should work work:


Code:
stty -icanon min 0 time 1

key=`dd count=1 2>/dev/null`

...

It should be able to read entire keystrokes at once.
Sponsored Links
    #6  
Old 03-26-2013
wisecracker's Avatar
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 23 October 2014, 3:18 PM EDT
Location: Loughborough
Posts: 642
Thanks: 133
Thanked 136 Times in 113 Posts
Hi rbatte1...

Quote:
Originally Posted by rbatte1 View Post
What's wrong with

Code:
stty raw -echo
inkey=`dd bs=1 count=1 2>/dev/null`
stty -raw echo

It is NOT a callable one liner function?

;o)

Thanks to Corona688 and his knowledge...

Here we go a single line function:-


Code:
#!/bin/bash

inkey() { char="" ; stty -icanon min 0 time 1 ; char=`dd count=1 2>/dev/null` ; }

while true
do
	printf "Some prompt:- "
	# This is LET char$=INKEY$...
	inkey
	printf "Key pressed:- '$char'...\n"
	if [ "$char" == "q" ]
	then
		printf "Quitting... \n"
		break
	fi
	if [ "$char" == "" ]
	then
		printf "Timeout works OK...\n"
	fi
	if [ "$char" == "b" ]
	then
		printf "Barry Walker...\n"
	fi
done

Bazza...

Last edited by wisecracker; 03-26-2013 at 02:05 PM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash function yanglei_fage Shell Programming and Scripting 5 12-20-2012 10:34 PM
BASH function error msarro Shell Programming and Scripting 1 07-13-2011 10:19 AM
bash search function doze Shell Programming and Scripting 3 09-30-2008 05:06 PM
Need help in using power function in Bash ahjiefreak Shell Programming and Scripting 3 03-26-2008 04:31 AM
Need assistance with bash function _jade_ Shell Programming and Scripting 1 03-05-2008 05:02 PM



All times are GMT -4. The time now is 08:39 AM.