[Keyboard] detect some special key in GP

Questions about GP commands and how to do things

Moderator: MSandro

Post Reply
MSandro
Posts: 162
Joined: Jul 26th, '17, 21:12

[Keyboard] detect some special key in GP

Post by MSandro » Sep 20th, '17, 15:52

Hi, currently I am trying to create a small user interface wich should contain some text fields to input some text and I don't want to use the "ask" block.
A view weeks ago I've released a small project named "simple text writer / notepad" wich works very well, but I can not detect if the user want to type some punctuation marks like ",.-;:_+#*'".
The next problems are the multiple keyboard layouts, like the english and the german one.
GP doesn't recognizes if I press "y" so it outputs "z" (QUERTY and QUERTZ).

Okay if GP would recognize all keys on an QUERTY keyboard correctly, it would be aczeptable (for the beginning) but currently GP doesn't!

Code: Select all

whenKeyPressed 'any' 'key'
self_setStageColor (colorHSV ((at (codePoints key) 1) / 4) 1 1)
self_say key
if ((containsSubString key 'arrow' 1) == 0) {
  if (key == 'space') {
    setShared 'text' (join (shared 'text') ' ')
  } (key == 'enter') {
    setShared 'text' (join (shared 'text') (newline))
  } (key == 'tab') {
    setShared 'text' (join (shared 'text') '    ')
  } (key == 'delete') {
    setShared 'text' (joinStrings (copyFromTo (letters (shared 'text')) 1 ((count (letters (shared 'text'))) - 1)))
  } (and (contains (range 1 9) (toNumber key)) (keyIsDown 'shift')) {
    setShared 'text' (join (shared 'text') (string ((at (codePoints key) 1) - 16)))
  } else {
    if (keyIsDown 'shift') {
      setShared 'text' (join (shared 'text') (toUpperCase key))
    } else {
      setShared 'text' (join (shared 'text') key)
    }
  }
}
}
This is the main part of my "writer script" to detect the keys. I think the script should be okay, but when I press "F1" it outputs "p". if I press "5" on my nummpad it outputs "]", the key "END" outputs an "m".

What is wrong with moy script or the way how GP is detecting keys???
Please help!

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: [Keyboard] detect some special key in GP

Post by JohnM » Sep 20th, '17, 19:27

The latest version of GP, v75, now reports symbol keys if you select "any" in the menu. The symbol key is available as the "key" parameter. Here's a test:
Screen Shot 2017-09-20 at 3.20.01 PM.png
Note that the "when key pressed" block is meant for using keys to control things like games (rather than text input), so the "key" is always the unshifted version of the key. For example, for number keys, it will always be a digit, not the symbol that is the shifted version of the key.

However, many accented characters are typed as a sequence of keys, and many Asian languages require multiple keystrokes per character. So, what you really want is a block that gives you the character(s) that should be inserted into the text, not the keys the user typed. GP does not (yet) have a block for that, but it's clearly needed, so I'll work on adding it. Thanks for making me aware of the need for it.

MSandro
Posts: 162
Joined: Jul 26th, '17, 21:12

Re: [Keyboard] detect some special key in GP

Post by MSandro » Sep 20th, '17, 20:36

Thank you very much for the realy fast improvements.

Post Reply