REBOL 3 - How to add an event handler to print a key that is pressed?

273 Views Asked by At

My end goal is to have a handler that will do something if I press a key such as #"1"

I have been reading the R3 View - Event Handling document, and trying to run the code in R3 View, I get no results. This is the handler I have copied:

my-handler: [
    name: 'my-handler
    priority: 50
    handler: func [event] [
        print ["event:" event/type event/offset]
        if switch event/type [
            close [true]
            key [event/key = escape]
        ] [
            unhandle-events self
            unview event/window
            quit
        ]
        show event/window
        none
    ]
]

I have tried both methods to run the event handler, with no results:

handle-events my-handler
view layout [button]

and

view/options layout [button] [handler: my-handler]

I'm at a loss for what to do, could someone point me in the right direction?

Here is my attempt at making a handler for a key:

view/options [button] [
    handler: [
        name: 'my-handler 
        priority: 50 
        handler: func [event] [
            if (event/key = #"1") [print "hi"]
        ]
    ]
]

As far as I can tell, it doesn't do anything...

1

There are 1 best solutions below

0
On

This should work with the published saphirion r3-gui, but is very crude.

load-gui  

ending: :halt  ; :quit  

base-handler: context [ ; we overwrite the default
    do-event: func [event] [
    ;   print "(Missing event handler)"
        if switch event/type [
           key [  ; this we add
            ; if event/key = #"1" [print "hi"]
              escape = probe event/key
           ] 
           close [ true]
        ] [
           unhandle-events self
           unview event/window
           ending
        ]
        event
    ]
    win-gob: none
    status: 'made
    name: 'no-name
    priority: 0
    about: "Main template for VIEW event handlers."
]

view/options [button] [handler: [] ]; this is not correct, so the base-handler is used