wxWidgets keyboard event - c++

I have a problem with using Key event:
there is example in documentation:
void OnChar(wxKeyEvent& event, double moveSpeed)
{
wxChar uc = event.GetUnicodeKey();
if ( uc != WXK_NONE )
{
...
}
else // No Unicode equivalent.
{
// It's a special key, deal with all the known ones:
switch ( event.GetKeyCode() )
{
case WXK_LEFT:
case WXK_RIGHT:
break;
case WXK_F1:
break;
}
}
but what if i have to use keyboards event in different function ?
I know that in other library you can simply use:
if (keyDown(SDLK_DOWN))
{
//code
}
but how can I use that in wxwidgets ?

You can use wxGetKeyStats() function if you really need to, but usually you definitely want to react to keyboard events instead of checking for the key state.

Related

Check Q button is held down using GLUT?

What function can I use to check if the Q button (or any other button is pressed down) and what will be the value needed to specify this for the Q button?
Using glut you need to define a Keyboard Handler function, and tell GLUT to use it for handling key strokes. Something along the lines of:
bool is_q_pressed = false;
void KeyboardHandler(unsigned char key, int x, int y)
{
switch (key)
{
case "q":
case "Q":
is_q_pressed = !is_q_pressed;
if (is_q_pressed)
// do something... or elsewhere in program
break;
}
}
void KeyboardUpHandler(unsigned char key, int x, int y)
{
switch (key)
{
case "q":
case "Q":
is_q_pressed = false;
break;
}
}
int main()
{
// Other glut init functions...
...
// Keyboard handler
glutKeyboardFunc(KeyboardHandler);
// Keyboard up handler
glutKeyboardUpFunc(KeyboardUpHandler);
...
glutMainLoop();
return 0;
}
EDIT: Added support for keyboard up. Using global variables isn't the best practice, but GLUT almost forces you to use them to keep track of program states. The good thing is that you can use the global variable (is_q_pressed) anywhere in your program... like in the idle() for some logic, or in the draw function to draw something if that key is pressed.
And, as #aslg said, you can make an array of bools to keep track of every key pressed, check his answer for ideas too :)
The simplest thing you can do is to use an array of bools with enough bools to contain the 256 regular keys and the special keys (right, left, etc.. ).
bool keys[256];
Use the KeyDown func to set the matching key to true, and KeyUp to set false.
void KeyboardDown( int key, int x, int y ) {
if ( isalpha( key ) ) {
key = toupper( key );
}
keys[ key ] = true;
}
void KeyboardUp( int key, int x, int y ) {
if ( isalpha( key ) ) {
key = toupper( key );
}
keys[ key ] = false;
}
The toupper just makes sure that pressing q or Q is the same whether Caps-lock is on or off. You don't have to use it if you don't need it.
Then somewhere in the update code you can check if a key was pressed like
if ( keys['Q'] ) {
// do things
}

Qt - know whether a pressed key is just a modifier

I'd like the child's handling of QKeyEvent not to be executed when it's just a modifier key that's being pressed. The following code does the job, but it's unwieldy to list them all like that. Is there a built-in way to do that?
void TextEditor::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Shift:
case Qt::Key_Control:
case Qt::Key_Alt:
case Qt::Key_Meta:
case Qt::Key_Mode_switch:
return QPlainTextEdit::keyPressEvent(event);
}
// handle the event...
}
You can use this QKeyEvent::modifiers() member for this purpose. If the result is
const bool isModifier = ( event->modifiers() != Qt::NoModifier );
true then the pressed key was a modifier.
So for you code it means the following modifications.
void TextEditor::keyPressEvent( QKeyEvent* event )
{
if ( event->modifiers() != Qt::NoModifier )
{
return QPlainTextEdit::keyPressEvent(event);
}
// Handle the event ...
}
Or if you want to handle some special key combination why not just use this way:
void TextEditor::keyPressEvent( QKeyEvent* aKeyEvent )
{
if ( aKeyEvent->matches( QKeySequence::Copy ) )
{
// Your copy stuff ...
return;
}
// else if ( aKeyEvent->matches( ... ) ) // Other key combinations ...
return QPlainTextEdit::keyPressEvent( aKeyEvent);
}
a better way to do this in 3 lines:
if(event->modifiers()){
event->ignore(); // or don't ignore and handle the event
// switching the modifiers
}
this is the modifiers defined by qt:
Qt::NoModifier
Qt::ShiftModifier
Qt::ControlModifier
Qt::AltModifier
Qt::MetaModifier
Qt::KeypadModifier
Qt::GroupSwitchModifier
you can do a switch for filtering.

Better implantation of key pressed down

I am currently executing a GetAsyncKeyState event handler when the 'c' key is pressed down in C++.
Here is my code:
bool isKeyPressed = false;
void someFuntionOne()
{
if( GetAsyncKeyState( 'C' ) & 0x8000)
{
if(isKeyPressed)
{
isKeyPressed = false;
}
else
{
isKeyPressed = true;
}
}
}
void someFunctionTwo()
{
if(isKeyPressed)
{
// Do something
}
}
So bassically I want to know if the 'C' has been pressed not held down so I use my boolean variable isKeyPressed to tell me if the key was pushed down at any point. If it was pressed, set isKeyPressed to true and if it was pressed again set isKeyPressed to false.
The problem I have with this is I am running a huge OpenGL/C++ program that takes a lot of time to go around and execute the entire program in a single iteration so if the user presses down on the 'C' key, the program may not catch that event in time because it is still executing the rest of the other code and is currently not at the section of the code where it checks if GetAsyncKeyState( 'C' ) & 0x8000.
Is there a better way I can check for this event where the program is always listening for the event to happen at any point of execution time? How can I implement this?
Usually whoever delivers the events to your application will queue them until the application has the chance to process them, i.e. at the end of each main loop iteration. So ideally you would react to the events at that stage. An (ugly) alternative is something like:
bool keyWasPressed = false;
void processKeyEvent() {
if( <C is pressed>) {
keyWasPressed = true;
}
}
void someFunction() {
if(keyWasPressed) {
keyWayPressed = false;
// Do something
}
}

Handling key events in SDL and C++

I'm in the process of migrating a program from GLUT to SDL. In my current program pressing the a key results in a different response then pressing the A key. This was pretty straightforward to do in GLUT as it the keyboard function callback passed in the ASCII value of the key that was pressed.
void keyPressedFn(unsigned char key, int x, int y){
switch(key){
case 'a':
// do work for a
break;
case 'A':
// do work for A
break;
}
}
I'm struggling to replicate similar functionality in SDL as pressing the a key produces the same response regardless of if SHIFT or CAPS LOCK are pressed as well.
Is there a simple way of replicating the above function in SDL?
Edit: In the example above I only show how to handle one key, in practice however, I am going to have a list of about 15 keys that I want to respond to differently if the shift key is pressed as well.
Check the keyboard modifiers that are present when you get a keydown event. For example:
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_a)
{
if(event.key.keysym.mod & KMOD_SHIFT)
{
// Handle 'A'
}
else
{
// Handle 'a'
}
}
break;
...
}
}
SDL_keysym has mod field, which contains state of modifier keys at the time the event has been emitted. Bit-AND it with KMOD_SHIFT to check whether Shift has been active. See SDL wiki.
Why not just do this?
void keyPressedFn(unsigned char key, int x, int y){
switch(key){
case 'a':
case 'A':
// do work for A or a
break;
}
}
Do you have some other concerns that you cannot do as what I have suggested? If not I think this is as simple as it can get.

How to make keyword to work instantly with no delay in Qt::4.6 (C++)?

OS:: win xp sp3.
Qt:: 4.6
I have class Gameboard in which i have some rectangle.I defined keyPressEvent for that rectangle in order to move him around the screen.Key_A :: rectangle.moveToLeft & Key_D :: rectangle.moveToRight.Problem is that keys work with delay.
When i release one key and press another one it takes some time before that one begin to work.I checked Qt online documentation and now for that effect but dont now how to make those keys to work instantly without delay beetween them?
code snippet:
//in Gameboard class
ship = new QRect(x,y,w,h);
void Gameboard::keyPressEvent(QKeyEvent* event)
{
switch(event->key()) {
case Qt::Key_A :
{
x = x-10;
ship->moveTo(x,y);
break;
}
case Qt::Key_D :
{
x = x+10;
ship->moveTo(x,y);
break;
}
}
}
Put input cursor into any applicable text box and press the 'A' key. What you'll see is once you press the key, letter 'A' will be printed, then there will be a pause, and then first letter 'A' will be quickly followed by many others. That's the way keypress events work. And your program is receiving them exactly like this. First you receive one event when the key is actually pressed, and then after a delay you get a lot of automatically repeated events, in case user wants to enter one character many-many times.
It works perfectly for text input, but in games you usually need smooth movement. If that's the case, you need to move your ship not upon receiving the event, but regularly, for example, on timer event. And you will need to catch both keyPressEvent and keyRelease event and use them to remember what movement keys are currently pressed. So you could for example do this:
struct Ship {
bool is_moving_left, is_moving_right;
QPoint position;
int speed;
...
void timerEvent()
{
if (is_moving_left) position.setX (position.x() - speed);
if (is_moving_right) position.setX (position.x() + speed);
}
...
};
...
void Gameboard::keyPressEvent (OKeyEvent *_event)
{
switch(event->key()) {
case Qt::Key_A :
ship->is_moving_left = true;
break;
case Qt::Key_D :
ship->is_moving_right = true;
break;
}
}
...
void Gameboard::keyReleaseEvent (OKeyEvent *_event)
{
switch(event->key()) {
case Qt::Key_A :
ship->is_moving_left = false;
break;
case Qt::Key_D :
ship->is_moving_right = false;
break;
}
}
Then just make sure Ship::timerEvent() gets called on every timer event in the game.