GLUT keeping track of pressed keys (when a key was pressed with SHIFT and released without it) - c

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.

Related

Why there is no SDLK_COLON equivalent scancode in SDL2?

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).

How can I recognize keys from a second keyboard?

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.

Codename One - Key binding for "p" is not working?

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.

How to use two keys of keyboard simultaneously in sdl

I'm writing a simple game in C with SDL and I have defined that player one controls it's_for example_tank with arrow key of the keyboard and player two controls it's tank with the keys "W","A","S","D" of the key board.
My problem is they can't press the keys simultaneously.
I have a function called "handle_events" that controls the events in the infinite while of the game and it's code is like below:
int handle_events(Tank* tank,Tank* tanker, Wall walls[]){
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
return EXIT;
if (event.type == SDL_KEYDOWN){
if(event.key.keysym.sym==SDLK_UP){
move_tank_forward(tank,colliding(tank,walls));
}else if(event.key.keysym.sym==SDLK_DOWN){
move_tank_back(tank,colliding(tank,walls));
} else if(event.key.keysym.sym==SDLK_RIGHT || event.key.keysym.sym==SDLK_LEFT) {
turn_tank(event.key.keysym.sym, tank);
} else if(event.key.keysym.sym==SDLK_w){
move_tank_forward(tanker,colliding(tanker,walls));
} else if (event.key.keysym.sym==SDLK_s){
move_tank_back(tanker,colliding(tanker,walls));
} else if(event.key.keysym.sym==SDLK_d || event.key.keysym.sym==SDLK_a){
turn_tank(event.key.keysym.sym, tanker);
}
}
I'm looking for a way that two players can play simultaneously because right now if for example both players press the up key and the w key, none of them can move their tank.
It appears you rely on key repeat - feature of operating/windowing system to repeat last pressed key with fixed interval after some initial pause, e.g. when you keep key pressed in text editor it starts filling that symbol after some delay. Only last key is repeated though.
With SDL, you should know when key is repeated from repeat field of keyboard event. However, you almost never want to make prolonged movement based on keypress event. With your current scheme, I can press a key very quickly and tank will move faster, or I can modify my OS repeat interval. Movement, if supposed to be at constant speed, should rely on time, not how fast user can press a button. That is why you only need to take nothion of "ok, button A pressed, - as long as it pressed, I will attempt to rotate left at each fixed update interval", and when you get key-released event - you drop that intention.
As Danny_ds said, it is often much easier and robust to use key state array instead of events (mostly because of repeat) - with SDL, you can get that via SDL_GetKeyboardState. Make your update_movements or whatever function, and call it every N miliseconds.
On Windows you can use the GetAsyncKeyState() function.
For an equivalent on Linux see this question.

SSMS - Changing keyboard shortcut for comment selection

So I wanted to change the keyboard shortcut that's assigned to "Comment Selection" to something that makes sense for me. I don't use many keyboard shortcuts, mainly because I have a hard time remembering all of them.
So, I wanted to change the one that is assigned to "Comment Selection". The default is Ctrl + K, Ctrl + C. I don't like the concept of having to do "2" keystroke combinations to accomplish this. I want to be able to quickly hightlight, enter the keystroke, and then move on, all while leaving one hand on mouse, and one on keyboard.
Too much info...
I changed my keystroke to be Ctrl + Shift + C, but the "event" never fires. It shows up in the options "Edit.CommentSelection" just fine. I overwrote the previous 3 values ("global", "text editor", "data warehouse designer"). I see there are other events that fire when Ctrl + Shift combination is used, so that rules out the possibility of not being able to use the Shift key, which was my original thought.
Any other suggestions on how to get this working for me?
From the discussion above, it seems that the shortcut was already assigned to Edit.CopywithHeaders (which is the default assignment for that shortcut). Removing that assignment will fix your problem.

Resources