How do I use QKeyEvent::key() even if window is not on top?
-
@SnuggleKat
Hi
The trick is always check which library to link to
Then add
LIBS += -luser32
to the .pro file.
-
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
LIBS += -luser32
Thanks, but the problem still remains the same
-
@SnuggleKat
Remember to run qmake after adding it
Up in the build menu. -
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
@SnuggleKat
Remember to run qmake after adding it
Up in the build menu.Thanks. I could compile it now. But the key value doesn't get checked. Could this issue be caused by the function being inside called up by a QTimer?
If I put the function into the MainWindow::MainWindow the RegisterHotKey permanently returns TRUE -
Hi
You mean from a timer slot ?
Should not make a difference however if you call it multiple times t without -
UnregisterHotKey , it might considered it already registered and return false.
you can show what GetLastError says in that case. -
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
Hi
You mean from a timer slot ?
Should not make a difference however if you call it multiple times t without -
UnregisterHotKey , it might considered it already registered and return false.
you can show what GetLastError says in that case.Well, I got it working while the window is on top. But once I leave the window the button isn't registered anymore.
RegisterHotKey(NULL, 0, MOD_IGNORE_ALL_MODIFIER, 0x42); MSG msg; GetMessage(&msg, 0, 0, 0); PeekMessage(&msg, 0, 0, 0, 0x0001); if(msg.wParam == 0x42) //0x42 is 'b' { ui->label->setText("true"); *(cheat_array + 16) = 0x11; } else { ui->label->setText("false"); *(cheat_array + 16) = 0x00; }
-
Hi
You are using NULL for windows handle
Docs says
"If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop."So i am wondering if it gets delivered by Qt when its not active.
Try to use its
(HWND)winId();
instead of NULL -
-
@SnuggleKat
Ok, i think its related to how the events are sent when windows not
in focus.try use eventfilter for native events and see if it comes there when not in focus.
https://forum.qt.io/topic/32640/help-with-qabstractnativeeventfilteryou can also try this sample ( non qt) and see if that works in all cases.
Its uses native msg pump.#include <stdio.h> #include <tchar.h> #include <windows.h> int main() { enum{ONE_KEYID = 1, TWO_KEYID = 2}; RegisterHotKey(0, ONE_KEYID, MOD_NOREPEAT, 0x31); // register 1 key as hotkey RegisterHotKey(0, TWO_KEYID, MOD_NOREPEAT, 0x32); // register 2 key as hotkey MSG msg; while(GetMessage(&msg, 0, 0, 0)) { PeekMessage(&msg, 0, 0, 0, 0x0001); switch(msg.message) { case WM_HOTKEY: if(msg.wParam == ONE_KEYID) { printf("1 Pressed"); } else if(msg.wParam == TWO_KEYID) { printf("2 Pressed"); } } } return 0; }
-
@mrjj said in How do I use QKeyEvent::key() even if window is not on top?:
@SnuggleKat
Ok, i think its related to how the events are sent when windows not
in focus.try use eventfilter for native events and see if it comes there when not in focus.
https://forum.qt.io/topic/32640/help-with-qabstractnativeeventfilteryou can also try this sample ( non qt) and see if that works in all cases.
Its uses native msg pump.#include <stdio.h> #includ...
My recent code update was based on this example.
Tried rebuilding it but didn't work either.And I'm a little bit lost about the EventFilter thing..
-
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?