0

In my GUI, I want every keyboard key (at least the letters, numbers and symbols), when pressed, to result in the next letter of my custom text being displayed, rather than resulting in the typed key being displayed.

For instance, if I set the text to be:

Bash is an sh-compatible command language interpreter

and I type:

29vVXKJmTQ3JXJ3jvUNHGRmC4A4ncK

I want to see appearing:

Bash is an sh-compatible comma

(and the next key pressed, regardless of which it is, should display "n".)

Which strategy can I use to do a map between any typed key and a custom text?

  • 2
    Smells like another recent question... Is this some bizarre assignment? – Stephen Kitt Dec 11 '16 at 21:49
  • What do you mean by GUI in his context? For a terminal window you could just read a character in raw mode and echo echo the next character from the string. For a generic X application you would probably want to create a large invisible window covering the whole screen, read keyboard events and pass on modified keyboard events. What happens if the user changes the input focus, do you in effect ignore this? – icarus Dec 11 '16 at 22:14
0

Share and enjoy:

#!/bin/bash

TYPEDSTRING=""
COVERSTRING="hello world"

POS=0

while IFS= read -r -n 1 -s X
do
  if [ "$X" == "" ]
  then
    break
  fi

  if [ "$X" == $'\x7F' ]
  then
    echo -n -e '\b \b'
    if [ "$POS" != "0" ]
    then
      POS=$(( (POS-1) ))
      TYPEDSTRING="${TYPEDSTRING:0:POS}"
    fi
  else
    echo -n "${COVERSTRING:POS:1}"
    TYPEDSTRING="${TYPEDSTRING}$X"
    POS=$(( (POS+1) ))
  fi
done

echo
echo "'$TYPEDSTRING'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.