Tuesday, 10 January 2012

VID keycodes and shortcuts

Sometimes a GUI needs a key shortcuts, for example CTRL+S to save a file, how to do this job with Rebol?
It's easy, just use the key word:

view layout [
text "Press A or B keys"
key #"a"   [ alert "You pressed a"]
key #"A"   [alert "You pressed A"]
key keycode [#"b" #"B"] [alert "You pressed b or B"]
]


The word keycode is used for more than one key or for special keys:
view layout [
text "Press PageUp or PageDown keys"
key keycode [page-up page-down] [alert "You pressed PageUp or PageDown"]
]

The CTRL button is represented with the letter pressed and the symbol ^:
view layout [
text "Press CTRL+S"
key #"^S" [alert "You pressed CTRL+S"]
]

Special key table:
KeyCode
Insinsert
Canc#"^~"
Home home
End end
PageUp page-up
PageDownpage-down
Left arrowleft
Right arrowright
Up arrowup
Down arrowdown
Space#" "
TAB#"^-"
CTRL+S#"^S"

If you need to intercept also SHIFT with CTRL, is possible, but a little more complicated; you need to use the event:
view layout [
the-box: box "A Box" forest feel [
engage: func [face action event] [
if   event/shift and (event/key = #"^S")   [ print "You pressed CTRL+SIFT+S"]
]]
do [focus the-box]
]

event/shift and event/control check SHIFT and CTRL keys

No comments:

Post a Comment