domeengine/dome

Expose SDL Keycodes

Closed this issue · 3 comments

Currently getting user input is a bit of a mess if they need to write something. This is due to the SDL scancodes being exposed instead of / without the keycode (or the direct UTF-8 character).

By exposing the SDL keycodes it's possible to see if a keypress is within a range instead of checking a map, list or having multiple if-statements.

An example of this could be the following I'm using for a TextBox:

_allowedKeys = [
      "a","b","c","d","e","f","g","h","i",
      "j","k","l","m","n","o","p","q","r",
      "s","t","u","v","w","x","y","z",
      
      "0","1","2","3","4","5","6","7","8",
      "9"
    ]
      
_convertKeys = {
  "space": " ",
}

// more code here

for (entry in keys) {
  // TO-DO: Add support for space and other symbols
  if (entry.value.justPressed) {
    // TO-DO: Add uppercase/lowercase support
    
    if (Keyboard.isKeyDown("backspace")) {
      deleteChar_()
    } else {
      if (Keyboard.isKeyDown("CapsLock")) {
        uppercase = Keyboard.isKeyDown("CapsLock")
      } else {
        if (Keyboard.isKeyDown("Left Shift")) {
          uppercase = !uppercase
        }
        
        var key = entry.key
        
        if (_convertKeys.containsKey(key)) {
          insertChar_(_convertKeys[key])
        } else if (key == "left" || key == "right") {
          moveCursor_(key)
        } else if (isCharAllowed_(key)) {
          insertChar_(key)
        } 
      }
    }
  }
}

SDL actually has a "text input" system, which probably fits our usecase better, but I haven't figured out the best way of hooking into it yet, if the intention is specifically for capturing typed text.

Yeah that'd fit much better, converting letters to upper/lowercase is also difficult with the current system unless someone has made a map for the corresponding values.

Upper/lowercase is a tricky problem once you begin dealing with unicode, so it's unlikely DOME will ever supply that. However, I am still interested in developing a "text input" system, once I get an idea of how it works in SDL, as well as how we might want to use it in applications.