I'm trying to check if the colon key is pressed, I'm using a English keyboard which means colon is shift + ;, my though was to read the keyboard state and then check the status of the scancode:
SDL_PumpEvents();
const uint8 *keyState = SDL_GetKeyboardState(NULL);
printf("keystate %d\n", keyState[scancode]);
but then I realized there is a scancode for the keypad colon SDL_SCANCODE_KP_COLON equivalent toSDLK_KP_COLON, but there is no scancode for SDLK_COLON (In case you are wondering: I tried with the keypad version and is not working).
So, I wonder why there is no equivalent scancode for SDLK_COLON and what's the best way to do this instead?
There is no colon scancode because there is no key on an ANSI (US) layout keyboard that produces a colon without modifiers. SDLK_COLON exists because there are layouts where a key produces a colon without modifier, like the French layout where the key corresponding to SDL_SCANCODE_PERIOD (dot . or greater-than sign >) makes a colon : or a slash / (with shift). So if you press that key on a French keyboard, you receive an event with SDL_SCANCODE_PERIOD and SDLK_COLON.
What I assume you want is to know if the user typed a colon, which has nothing to do with scancodes or even keycodes when dealing with different keyboard layouts. For that, you need a SDL_TextInputEvent. Check out the wiki's tutorial about it.
Or you could use SDL_SCANCODE_SEMICOLON, check what that key corresponds to for the user, and display that so the user knows what key to press (it would be the รน key for a French keyboard).
Related
I bought a second keyboard, USB with 20 programmable keys. I'm using Windows.
The programming utility foresees using it for gaming, for macros, or as a number keypad, but I need it for another script - I don't want to duplicate keys I already have on my normal keyboard.
I notice that normal keycodes for the function keys F1-F12 are 112-123, but this utility offers me function keys F1-F20, presumably with codes 112-131. For my 20 extra keys, I'd like to use 124-143, as if they were F13-F32: I want to be able to recognize keys from the second keypad unambiguously. NumLock is 144, but there doesn't seem to be any codes in my range.
But is there a correct or better way to do this? I can't be the first person to want to add more keys to a keyboard.
I am trying to attach Commands to buttons. This method works for 'q' and 'm', but it is not working for 'p' character.
Using '-112' did not solve the problem either. using minus symbol works for arrow keys but not for 'p'.
addKeyListener(113, new CommandA(gameWorld)); // q
addKeyListener(109, new CommandB(gameWorld)); // m
addKeyListener(112, new CommandC(gameWorld)); // p - Not Working
pressing 'p' does nothing.
UPDATE
Found a workaround...
This won't work for you on the device anyway as most devices are touch devices. The virtual keyboards on such devices don't send these events anyway. The arrow keys send undocumented values which differ a lot and you should "theoretically" use the game key API and not the key API. But again this won't work on the devices most commonly used today.
The characters aren't just numbers. They are literally the character value (int)'p'. There is no special treatment for them but it's possible that something in the system is grabbing that specific character as it's commonly associated with a print shortcut.
I am trying to keep track of the pressed keys. I have glutIgnoreKeyRepeat(1) and I register the key events (down/up for normal and special) in an object that keeps track of the keys pressed (called the list of pressed keys from now on) so that I just check in my display function if a key is pressed by looking in that list.
The problem is with SHIFT: Different keys register as DOWN and UP (depending on whether SHIFT was pressed or not) for the same physical keyboard key (eg.
a and A, 1 and !, [ and { etc.)
Consider this (with {key1 key2} representing my list of pressed keys after the event):
initial state : {} no keys listed as pressed
SHIFT DOWN : {SHIFT}
a DOWN : {SHIFT A} (because SHIFT is still down, capital 'A' registers as the key down.
SHIFT UP: {A}
a UP: {A} (because SHIFT is not pressed, lower 'a' registers as the key up, so a is removed from the pressed key list (which actually doesn't exist), and A still remains)
final state: {A} even though no keys are still pressed on the keyboard.
For alpha keys I can solve this by adding/removing the lower case of the keys (a instead of A).
But what about 1 and ! or [ and {. glutGetModifiers doesn't help here. Or what if a key is pressed, the window looses focus, the key is released and the window gains focus again (there is no event to tell that the key was released when the windows lost focus)?
Is there a general solution to check if a key is pressed? (platform dependent solutions are ok, another library is not for me, but maybe for others who need to solve this)
What you want are the keycodes, the physical number of the key being pressed on the keyboard. These are different on Apple and PC keyboards, and can also be different on keyboards in other countries where the letters change positions.
Most programs don't care about keycodes, they want actual letters and modifier keys, so the system event handler provides a translation from keycode into the intended character/special key. GLUT is designed to be simple, portable, and cross-platform, so uses these routines.
If you're writing a game or simulator, just lowercasing everything is probably good enough. For that kind of high speed interaction, most players won't want to distinguish between 1 and !, or 2 and #. The Ins/Del and arrow key blocks (usually) don't have multiple symbols anyway, and the numeric keypad is too far away for most people to hold down a modifier key at the same time.
If you really, truly, need to track every key state, sorry GLUT won't work for you. On MacOS you ask the NSKeyEvent for the keycode, on MS Windows you use DirectX, on Linux you use the XKeyPress|ReleaseEvent keycode.
Hope this helps.
I want to create an accelerator for CTRL+A. I noticed that it doesn't work when you leave out FVIRTKEY, i.e. the following code snippet doesn't work:
a.fVirt = FCONTROL;
a.key = 'A';
a.cmd = IDM_ADD;
(just for completeness' sake: using 'a' instead of 'A' doesn't work either)
This, however, works fine:
a.fVirt = FCONTROL|FVIRTKEY;
a.key = 'A';
a.cmd = IDM_ADD;
Can somebody explain this behaviour? MSDN says that if FVIRTKEY isn't set, "key" is interpreted as a character code which I presume to be ASCII. But it doesn't work which leaves me somewhat puzzled.
Thanks!
The system translates certain Ctrl key combinations into ASCII control codes. The combination Ctrl+A is translated to the ASCII ctrl-A (SOH) character (ASCII value 0x01). This is the reason why your first snippet does not expose the desired behavior: It requires input that is impossible to enter. This is documented under Keyboard Input in the MSDN.
Also note that not providing the FVIRTKEY flag makes the specified key case-sensitive, i.e. 'a' and 'A' map to different input. It is usually desirable to use virtual key codes, to provide a consistent keyboard UI. The implications are explained under About Keyboard Accelerators - Accelerator Keystroke Assignments.
I have a small WIN32 C-Application in which i work with the KBDLLHOOKSTRUCT structure. This structure contains the VK-Code for a pressed key.
I try to convert this to an ASCII-Character. For this i use the Function MapVirtualKey, which works well.
The only problem is, that one VK-Code can stay for multiple chars.
Example:
On my keyboard (Swiss-German) exists the key-char .. If i press Shift+. then it creates a :. The VK-Code is the same. Thats no problem, and i can also check if Shift is pressed or Caps Lock is activated.
My only problem is: How can i get the char ':'?
I need a function like this:
GetKeyChar(vkCode, shift)
I need this to get the "normal" and the "shifted" value of the keyboard. Of course i could hardcode this, but i don't like to do it on this way.
The problem is that the KBDLLHOOKSTRUCT doesn't have all the information you need in order to do the translation. You get a message every time a key is pressed. So for Shift+X, you'll get an input message saying that the Shift key was pressed, and another message saying that the "X" key was pressed.
You need to call GetKeyboardState in order to get the state of the Shift, Alt, Ctrl, (and perhaps other) keys. Then call ToAsciiEx or ToUnicodeEx.
You're looking for ToUnicode, which returns the unicode character generated by that keypress.
The functions you are looking for are: ToAscii, ToAsciiEx, ToUnicode, ToUnicodeEx.
short VkKeyScan(char ch) API has contained the shift information. It translate char to virtual-key code and shift state.
See this: Convert character to the corresponding virtual-key code