check_keypress()

new topic     » topic index » view thread      » older message » newer message

Bryan Watts wrote:
>. . . I was thinking something like:
>  check_keypress({y,n,q,b,t})
>and it would only return if you pressed any of those letters.  It could >be
used for menus or choices or whatnot.  It would go something like this:
>---------code is here----------
>include wildcard.e
>
>function check_keypress(sequence look_for)
>  integer k, m
>  look_for = lower(look_for)
>  while m = 0 do
>     k = lower(get_key())
>     m = match(k, look_for)
>  end while
>  return look_for[m]
>end function
>
     Bryan, you asked for suggestions on the above, so here is a slight
variation that might be more useful in some situations.  Also note that you
will get a couple of errors if you run the above code; m must be initialized
before tested in the loop and match() takes sequence arguments (I think)-k
is an integer.

--------------- code starts
include wildcard.e

global function wait_for(sequence goodchars)
-- slight variation of Bryan Watts' check_keypress()
-- returns index into choice list rather than character code
-- case insensitive, as was Bryan Watts' original
integer c, m

   goodchars = lower( goodchars )
   m = 0
   while not m do
      c = lower( get_key() )
      m = find( c, goodchars )
   end while

   return m
end function
------------- example test
sequence message
integer keyhit

message = {"Yes","No","Maybe"}

keyhit = wait_for("YnM")

printf(1,"You chose %s!\n",{message[keyhit]})
---------------------------------------------

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu