Quote:
Originally Posted by scriptless
Wait, capital letters, and lowercase letters have seperate keycodes aside from the fact you have to press shift, to achieve the capital letter??
I did the following,
PHP Code:
getKeycode("a"); // result = 65 getKeycode("A") // result = 65 keyName2("45"); //result = "insert"
As you can see it's not doing capital and lowercase it's doing which phisical button... what is it exactly you are trying to do maybe we can help guide you in another direction to accomplish this? Because it's already displaying which of the ~ 101-105 keys on the keyboard you pressed. To check if it's capital you need to know if the shift key was pressed or the cap lock was activated at the time of recording the key being pressed.
|
Retrieving "keycode" from:
function keyPressed(keycode, key, scancode) {
}
keycodes for lowercase letters and uppercase letters are off by 256. ie, if 'a' is 65, 'A' is 65 + 256. This is also true of 1 to ! and other symbols. Sure, I suppose with the proper tools you can probably BUILD the final keycode (set a var if shift is down or capslock is on, add 256 to the getkeycode if var is true, etc). But that's dumb -- why are the onKeyPressed and getKeycode/keyName2 functions NOT directly compatible with each other?
That said, typing "!" yields a keycode of whatever 1's keycode is plus 256.
However, doing getKeyCode("!") yields 0. It doesn't even yield the keycode for 1. So even if I'm supposed to "build" the keycode out of knowledge as to whether shift or capslock is on (not that there's any support for that...), I still couldn't build the value for ! unless I access the value for 1 and add 256. I couldn't unbuild it unless I accessed its original value (as opposed to just doing - 256).
Say for example one wanted to make an on-screen keyboard. Let's assume that person only wanted to use 1 array, an array of keycodes. What would be nice is something like:
keys = {"`", "1", "2"}; // define the keys of a QWERTY keyboard in order
Then loop keys, performing getKeycode(key) to build a 2nd array of the keycodes.
Then loop keys, performing getScancode(key) to build a 2nd array of scancodes (if needed/as needed)
Can't do that, because getKeycode(key) does not work for any shifted keys. Yes you could store what the base key is and for a shifted key you could add +256, but then the whole method is wash.
So instead you try:
keycodes = {num0, num1, num2, ...}; // array of the keycodes, in order of QWERTY
Okay, so now it's time to build your array. Except keyName2() does not return a different value between lowercase, uppercase, and returns null for a lot of symbols.
Okay, so the final method (arguably best method) is to use Scancode: scancodes start at 2 for 1, 3 for 2, etc. Nice thing is that they increase linearly, where 11 is 0, 13 is =, 14 is backspace, 15 is tab, 16 is q, etc. Looks like they're actually made for keyboards. You could probably just loop the scancodes to establish the bulk of a base keyboard, and then add the sides/top frills afterwards. You wouldn't even need a QWERTY array.
But there's no function to determine the scancode of a letter, and no function to determine the letter from a scancode.
getASCII()/char() is also a bust whenever any invisible symbol comes up.
So ultimately, the only method that would work by my estimation, is:
1. a big ass array of QWERTY letters
2. a second array of their shifted letters (because keyName2(keyCode(1) + 256) DOES NOT yield "!")
3. a corresponding array of keycodes (because getKeyCode("!") would yield null)
4. POSSIBLY ALSO an array of scancodes (such a scancode array COULD prove useful; the 2 shift's have the same keycode but different scancode, and all SPACEBAR ENTER, TAB, etc entries have the same scancode, while there are like 15 diff keycodes for them (depending on which mod keys are held down), so you'd have to do an "in" {} check if you wanted to determine them by keycode).
So that's like 4 arrays each having like 50 entries. I'm sure it works, but it looks lame. 1 array of qwerty letters that you could derive keycodes from (or visa-versa) + the ability to use math to shift a letter on-demand would make the whole thing nicer.
Unless you've got some other idea for an approach I haven't thought of?