File tree 1 file changed +58
-0
lines changed
1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ Rebol [
2
+ name: wait-for-key
3
+ title: "Simple Rebol `wait-for-key` function"
4
+ purpose: "Demonstrate how to get simple user keyboard input from console without need to press ENTER"
5
+ author: "Oldes"
6
+ needs: 3.12.0 ; https://github.com/Oldes/Rebol3
7
+ ]
8
+
9
+ wait-for-key : func [
10
+ "Wait for single key press and return char as a result"
11
+ /only chars [bitset! string! ] "Limit input to specified chars"
12
+ /local port old-awake
13
+ ] [
14
+ ; using existing input port
15
+ port: system/ports/input
16
+ ; store awake actor and enter turn off read-line mode
17
+ old-awake: : port/awake
18
+ modify port 'line false
19
+ ; store optional chars limit in port's extra
20
+ port/extra: chars
21
+ ; clear old data (in case user cancel's waiting)
22
+ port/data: none
23
+ ; define new awake, which checks single key
24
+ port/awake: func[event][
25
+ all [
26
+ 'key = event/type
27
+ any [
28
+ none? port/extra
29
+ find port/extra event/key
30
+ ]
31
+ event/port/data: event/key
32
+ true
33
+ ]
34
+ ]
35
+ ; wait for user input
36
+ wait /only port
37
+ ; put back original awake actor and read-line mode
38
+ port/awake: :old-awake
39
+ modify port 'line true
40
+ ; return result and clear port's data
41
+ also port/data port/data: none
42
+ ]
43
+
44
+ ;-- Usage example ------------------------------------------------------
45
+ print "Press any key!"
46
+ char: wait-for-key
47
+ print ["You pressed:" mold char]
48
+
49
+ print "Press ENTER!"
50
+ wait-for-key/only CRLF
51
+
52
+ print "Press any number!"
53
+ char: wait-for-key/only charset [#"0" - #"9" ]
54
+ print ["You pressed:" char]
55
+ print "Good choice!^/ "
56
+ print "That's all... good bye!"
57
+ wait-for-key
58
+
You can’t perform that action at this time.
0 commit comments