2

We can get input from console by input or ask, which means to press some keys on keyboard and terminate the input by pressing the key "Enter". I'd like to know if there is a way to get a key input, that is to say, just press a key on keyboard and then something happens, like this:

if read-key = #"a" [do something...] ;;if the key "a" on keyboard is pressed, something is done immediately--not by pressing the key "Enter" after pressing "a".

If no such any-function! like read-way in red/rebol, how to achieve it?

lyl
  • 293
  • 1
  • 8
  • 1
    Does this answer your question? [Is it possible to detect esc key with ask function?](https://stackoverflow.com/questions/4233670/is-it-possible-to-detect-esc-key-with-ask-function) – 9214 Apr 09 '21 at 08:58

1 Answers1

2

On Rebol2 this should work:

>> console-port: open/binary [scheme: 'console]
>> pick console-port 1 ; hit space
== 32

>> to char! pick console-port 1  ;hit x
== #"x"
endo64
  • 2,269
  • 2
  • 27
  • 34
  • A good solution! Thank you. And is there a Red version? – lyl Apr 09 '21 at 14:53
  • @lyl no, Red doesn't have ports ATM. Depending on the task, you may try to hack your way around with View event capturing. – 9214 Apr 09 '21 at 15:25
  • Thank you alll for these good solution and proposal! – lyl Apr 10 '21 at 00:43
  • @9214 Yes, `on-key` event in View is for this. But how to present keys combination, for example, `ctrl a` or `ctrl shift a` or `alt a`? – lyl Apr 10 '21 at 01:40
  • @lyl each event has a set of flags dedicated to that. See [View documentation](https://github.com/red/docs/blob/master/en/view.adoc#112-event-datatype). – 9214 Apr 10 '21 at 04:26