How do I use QKeyEvent::key() even if window is not on top?
-
The link have sample of such eventfiler.
its not much code.Its odd, the other sample didnt work as its windows less and have message pump.
But if u mix with a normal Qt it makes sense as the events are eating by QApplication then. -
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
The link have sample of such eventfiler.
its not much code.Its odd, the other sample didnt work as its windows less and have message pump.
But if u mix with a normal Qt it makes sense as the events are eating by QApplication then.I'm sorry, I can't get it to compile..
Could it be it doesn't work because the timer() function is a (public) slot?
-
@SnuggleKat
A slot is just a normal c++ function.But i dont understand why you need to call it in a timer.
You normally call it ONLY once to set up hot key.
Then reacts to WM_HOTKEY native event.
There would be no need of a timer.Mixing Qt application and GetMessage() might have side effects.
This might help
http://amin-ahmadi.com/2015/11/14/how-to-use-system-wide-hotkeys-in-your-qt-application/ -
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
@SnuggleKat
A slot is just a normal c++ function.But i dont understand why you need to call it in a timer.
You normally call it ONLY once to set up hot key.
Then reacts to WM_HOTKEY native event.
There would be no need of a timer.Mixing Qt application and GetMessage() might have side effects.
This might help
http://amin-ahmadi.com/2015/11/14/how-to-use-system-wide-hotkeys-in-your-qt-application/I need it in a timer because it executes the codehandler as many times a second as defined by the user.
However, I could also transfer the key_value or key_code over to the timer -
Ok. but try the sample i linked. it is using virtual override for native Event and it might work when app is not focused.
Else an event filter on application seems the best way to grab WM_HOTKEY. -
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
Ok. but try the sample i linked. it is using virtual override for native Event and it might work when app is not focused.
Else an event filter on application seems the best way to grab WM_HOTKEY.Wow, it works, thank you!
For some reason it makes the b-key not typing anything. is there are workaround? -
@SnuggleKat
Super :)
Some other app might have requested a hotkey on b.
So if be is not sent to you. it means something else takes it.
I think F12 also reserved and some others. -
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
@SnuggleKat
Super :)
Some other app might have requested a hotkey on b.
So if be is not sent to you. it means something else takes it.
I think F12 also reserved and some others.I see!
Well, it's not too bad though. I will upload a demo once I have made some more progress with my application -
@SnuggleKat
Super. please mark as solved if possible.
You can always open other if later questions comes up. -
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
@SnuggleKat
Super. please mark as solved if possible.
You can always open other if later questions comes up.Here's a little demo video!
https://twitter.com/CosmoCortney/status/929459711388344322Well, there came up another problem tho..
when I pass another key value to the RegisterHotKey() function any of the both values trigger both functions at the same time.
I think I should un-register the value after it's corresponding function was executed. but how do I do this? -
@SnuggleKat
Hehe cool. Not sure what the reindeer is doing though :)Hi
If you register 2 hotkeys. it will call nativeEvent twice regardless of which
key you press or what do u mean ?if you read the docs, it says
"lParam
The low-order word specifies the keys that were to be pressed in combination with the key specified by the high-order word to generate the WM_HOTKEY message. This word can be one or more of the following values. The high-order word specifies the virtual key code of the hot key."So maybe you just need to alter the code and only call your function depending on what actual hot key is?
Like
#define VK_M 0x4D #define VK_N 0x4E MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); if(!RegisterHotKey(HWND(winId()), 0, MOD_ALT | MOD_CONTROL, VK_M)) { QMessageBox::warning(this, "Warning", "Can’t register hotkey ALT + CTRL + M"); } if(!RegisterHotKey(HWND(winId()), 0, MOD_ALT | MOD_CONTROL, VK_N)) { QMessageBox::warning(this, "Warning", "Can’t register hotkey ALT + CTRL + N"); } } bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result) { MSG* msg = static_cast<MSG*>(message); if(msg->message == WM_HOTKEY) { WORD hotKey = HIWORD(msg->lParam); switch (hotKey) { case VK_M: QMessageBox::about(this, "", "VM_M pressed"); break; case VK_N: QMessageBox::about(this, "", "VM_N pressed"); break; default: break; } return true; } return false; }
-
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
@SnuggleKat
Hehe cool. Not sure what the reindeer is doing though :)Hi
If you register 2 hotkeys. it will call nativeEvent twice regardless of which
key you press or what do u mean ?if you read the docs, it says
"lParam
The low-order word specifies the keys that were to be pressed in combination with the key specified by the high-order word to generate the WM_HOTKEY message. This word can be one or more of the following values. The high-order word specifies the virtual key code of the hot key."So maybe you just need to alter the code and only call your function depending on what actual hot key is?
Thanks!
What i did was loading the reindeer's x, y and z size values (floats) and incremented it and stored it back to the game each time the timer repeated. This action was triggered by the hotkey =)I have modified the code a lot in order to work as I need it:
void MainWindow::if_key_pressed() // 080000XX 000000VV // this is what the cheat code layout looks like. VV = hotkey value // D00000XX BADF000D // this is the end-if code where to branch when the hot key is not pressed. { key_value = *(cheat_array + index_B + 0x07); ifnr = *(cheat_array + index_B + 0x03); RegisterHotKey(HWND(winId()), 0, 0, key_value); // register hotkey if(key_is_pressed == false) { index_B += 0x08; for(int i = 0, j = index_B; (*(cheat_array + j + i) != 0xD0) || (*(cheat_array + j + i + 3) != ifnr); i += 8) { index_B += 8; } } else { index_B += 0x08; } } //.... bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) { Q_UNUSED(eventType); Q_UNUSED(result); MSG *msg = static_cast<MSG*>(message); if(msg->message == WM_HOTKEY) // check if key is pressed { key_is_pressed = true; true; } else { key_is_pressed = false; false; } return false; } // if the key is pressed index_B will jump to the cheats to be executed (e.g. to grow the deer). if it's not pressed it jumps to the end-if to skip the cheats
so.. should I try clear msg?
-
Hi
ok. not sure what question is ?
code will do the same regardless of what you register as hotkey.
Is that what you wanted ? -
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
Hi
ok. not sure what question is ?
code will do the same regardless of what you register as hotkey.
Is that what you wanted ?Sorry for my absence, I had a busy month...
The problem is that once the key was pressed key_is_pressed stays true even when I release the key. So the function I call up with the pressed key keeps being active.
This is what I'd need to have fixed so I can control the flow of the code with the keys -
@SnuggleKat
Ok try my code. did not notice it always being true.
You register the shortcut on each key press. Its not how its ment to work.
(i call it once in constructor)Also you must use
WORD hotKey = HIWORD(msg->lParam);
and not lParam directly. -
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
@SnuggleKat
Ok try my code. did not notice it always being true.
You register the shortcut on each key press. Its not how its ment to work.
(i call it once in constructor)Also you must use
WORD hotKey = HIWORD(msg->lParam);
and not lParam directly.Thanks,
I had to modify it a little bit because the user can define which key to be checked (case labels don't like variables).bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) { Q_UNUSED(eventType); Q_UNUSED(result); MSG *msg = static_cast<MSG*>(message); if(msg->message == WM_HOTKEY) // check if key is pressed { WORD hotKey = HIWORD(msg->lParam); if(hotKey == key_value) { key_is_pressed = true; true; setWindowTitle("pressed"); } else { key_is_pressed = false; false; setWindowTitle("not pressed"); } } return false; }
It behaves weird now.
If I'm checking a certain user defined key value the function will stay on once the corresponding key was hit. (same as before)
If I'm checking for more than one value where each value triggers a different function the earlier noted values will turn off everything and the last one will turn all functions on.