Search code examples
rebolrebol3r3-gui

Text and caret in VID or R3-Gui


A simple example:

If I type #"w" in style "area" how do I get an #"z"? (ex. "qwerty ww" -> "qzerty zz")


Solution

  • As you want the conversion on the fly, you can either modify R3-GUI before loading. So load r3-gui.r3 down to your local directory. Then you add the line if key == #"w" [key: #"z"] to the function do-text-key, so it looks like

    do-text-key: funct [
      "Process text face keyboard events."
      face [object!]
      event [event! object!]
      key
    ] [
      text-key-map/face: face
      text-key-map/shift?: find event/flags 'shift
      if no-edit: not tag-face? face 'edit [
        key: any [select/skip text-key-map/no-edit key 2 key]
      ]
      either char? key [
        if key == #"w" [key: #"z"]
        text-key-map/key: key
        switch/default key bind text-key-map/chars 'event [
          unless no-edit [
              insert-text-face face key
          ]
        ]
      ] [
        if find event/flags 'control [
          key: any [select text-key-map/control key key]
        ]
          text-key-map/key: key
          switch/default key text-key-map/words [return event]
      ]
      none
    ]
    

    Probably the official way would be to use on-key wih Rebol3

    load-gui
    view [
      a: area  on-key [ ; arg: event
         if arg/type = 'key [
            if  arg/key == #"w" [arg/key:  #"z"]
         ]
         do-actor/style face 'on-key arg face/style
      ]
    ]
    

    And finally a way to do this with Rebol2 on the fly

    key-event: func [face event] [
        if event/type = 'key [ 
            if all [event/key = #"w"   ] [
                append a/text  #"z" 
                focus a
                view w 
               return false
            ]
        ] 
        event 
    ] 
    insert-event-func :key-event        
    
    view w: layout [
        a: area 
    ]