RegisterHotKey without a modifier in c++ - c++

This piece of code registers 2 global hotkeys under Windows for SHIFT+F5 and SHIFT+F6 it works fine in all cases, including the case that a completely other application has the focus like for example a game.
enum{ KEY_F5 = 1, KEY_F6 = 2 };
RegisterHotKey(0, KEY_F5, MOD_SHIFT, VK_F5);
RegisterHotKey(0, KEY_F6, MOD_SHIFT, VK_F6);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
PeekMessage(&msg, NULL, 0, 0, 0);
switch (msg.message){
case WM_HOTKEY:
if (msg.wParam == KEY_F5){
// code
}
else if (msg.wParam == KEY_F6){
// code
}
}
}
But if i replace
RegisterHotKey(0, KEY_F5, MOD_SHIFT, VK_F5);
RegisterHotKey(0, KEY_F6, MOD_SHIFT, VK_F6);
by
RegisterHotKey(0, KEY_F5, 0, VK_F5);
RegisterHotKey(0, KEY_F6, 0, VK_F6);
because my goal is it two have F5 and F6 as hotkeys without having to press SHIFT always, it only works if my program has the focus not if some other has. How can i achieve to have only F? as global hotkey like for example TS3 does. It must work even if the application doesn't have the focus.

Try use MOD_NOREPEAT instead of MOD_SHIFT. I also don't see any necessity of using "PeakMessage". Since you already call GetMessage(), PeakMessage is really redundant.

Related

RegisterHotKey() not detecting keypress

So, I am trying to write a program that detects when the keycombination Control + C is pressed, I have read that the best way to do this is to use RegisterHotKey(). I have never used this before though so I am a little confused since my code isnt working. This is the solution I found online but it doesn't work. I have looked at the documentation but that hasn't helped either. this is my code:
AutoClicker::AutoClicker()
{
clicker = std::thread(&AutoClicker::Clicker, this);
enum {KEY_C = 1};
RegisterHotKey(0, KEY_C, MOD_CONTROL, 0x5A);
MSG msg;
while (GetMessage(&msg, 0, 0, 0)) {
PeekMessage(&msg, NULL, 0, 0, 0);
switch (msg.message)
{
case WM_HOTKEY:
if (msg.wParam == KEY_C)
{
printf("1 Pressed");
}
}
}
}

auto click with C++

I write a simple cpp file when your left mouse button down it will click after 50 milliseconds and it works with many windows but when I click in the Tencent Gaming Buddy(an android emulator) it is not working - so how could I get the left mouse button down when I click here is my code
while (true)
{
Sleep(50);
if ((GetKeyState(VK_LBUTTON) & 0x80) != 0)
{
if (GetCursorPos(&p))
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
}
}
if ((GetKeyState(VK_RBUTTON) & 0x80) != 0)
{
break;
}
}
GetKeyState() relies on the internal key state machine of the calling thread, and does not work without an active message loop to update that state, which this code does not have. Use GetAsyncKeyState() instead.
Also, mouse_event() is deprecated, use SendInput() instead

C++ CreateWindow: button position gets offset at window maximize

I have a weird problem over here. I created a DLL proxy for Spotify so I can "overlay" a button onto it. Basicially, thats how it works:
DllMain
-> Creates CMain class
-> Creates CToggleButton class
-> Hooks the button onto the Spotify window
It has two methods, one static one which I use for the thread since threads can't call member functions, and one non-static function which gets called by the member function.
With this, I create the thread and pass an instance of the CToggleButton class via lpParam:
CreateThread(0, NULL, WindowThreadStatic, (void*)this, NULL, NULL);
Then, the WindowThreadStatic function:
DWORD WINAPI CToggleButton::WindowThreadStatic(void* lpParam)
{
return ((CToggleButton*)lpParam)->WindowThread();
}
And the main window thread function inside the class:
DWORD CToggleButton::WindowThread()
{
MSG msg;
hButton = CreateWindowA("BUTTON", "Test", (WS_VISIBLE | WS_CHILD), 0, 0, 100, 20, parenthWnd, NULL, hInst, NULL);
bool bQueueRunning = true;
while (bQueueRunning)
{
if (PeekMessage(&msg, parenthWnd, 0, 0, PM_REMOVE))
{
switch (msg.message)
{
case WM_QUIT:
bQueueRunning = false;
break;
case WM_LBUTTONDOWN:
if (msg.hwnd == hButton)
{
MessageBoxA(parenthWnd, "Button!", "Button", MB_OK);
continue;
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Sleep(10);
}
return 0;
}
As you can see, this also contains the message loop for the button (I didn't use GetMessage() here because it was very unresponsive so I decided to use PeekMessage() together with a 10ms delay, which works fine.)
Little picture to show how it looks like:
All great, but if I maximize the window, the button disappears. When I minimize and maximize the window a few times, the button can be seen again, but with very weird coordinates (not the original 0,0 I gave him).
So what is my problem here? Why do the coordinates get offset?
Thanks for reading my long post :)

createthread MFC dialog based app (Syntax)

I am trying to create a Worker thread but I have not done Visual C++ since 2004 and threading syntax has changed (Please don't worry about timing issues, I will have that covered). the issue is that in an MFC dialog app, every way I know of trying to create a thread won't get past the compiler. I also have tried the "&" trick. It can't seem to find this function at all. Can anyone help please? NOTE: I tried three methods and I left the first uncommented.
if (i_found >= 0) { //this is just a combobox snippet to show the calls
MessageBox(wch.GetBuffer(0), L"Port Select", MB_OK);
_serialPort->Close();
ConversationRight = gcnew Conversation(systrName);
_beginthread(&CDLP_Printer_ControlDlg::Mine_QL, 0, 0); //NONE WORKING!!
//unsigned long lpRecvThread = _beginthreadex(NULL, 0, Mine_QL, (void*)this, NULL, NULL);
//unsigned long lpThreadIdRequest;
//::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Mine_QL, (void*)NULL, 0, &lpThreadIdRequest);
}
else
MessageBox(L"Not the left board", L"Port Select", MB_OK);
_serialPort->Close();
}
void CDLP_Printer_ControlDlg::OnBnClickedBtnStop()
{
ConversationRight->WriteLn("5");
}
bool CDLP_Printer_ControlDlg::UpdateCommsWindow_left(String^ strCommsLine)
{
return false;
}
void CDLP_Printer_ControlDlg::Mine_QL()
{
//_endthread();
}

Win32 ReleaseDC and DestroyWindow returning 0

I have a single window created with the CS_OWNDC style for an engine I'm coding and it's showing properly, but when I'm closing the program and I call the ReleaseDC(hWnd, hDC) the function returns 0 always (and according to MSDN it means it couldn't be released).
The DestroyWindow(hWnd) is also returning 0.
I am calling GetDC(hWnd) during the window creation.
What is weird is that I only get this error if I pass through the messaging function, even if it has nothing coded inside.
My main code is looking like this:
WindowManager windowManager;
HWND mainWindow = windowManager.Initialize("Title", "ClassName", 1024, 768, 32, WindowProc);
MSG msg;
while(true)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT) { break; }
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
windowManager.Clear(); //this calls ReleaseDC(hWnd, DC) and DestroyWindow(hWnd) among other release codes
What I don't understand is that if I comment the whole while loop, I don't get the release or destroy window errors.
Calling GetLastError() gives me a 0 too.
On MSDN it says the DestroyWindow must be called from the same thread, but I'm not coding any multi thread functions so I don't think that's the case.
What could be causing this?