RegisterHotKey() not detecting keypress - c++

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");
}
}
}
}

Related

After check GetKeyState() in main message loop, application freezes until the second press of the same button

I need to create stop action event in main message loop.(ListBox after press VK_RIGHT moves down - and I don't want it to happen).
Another problem is application freezes until I press right arrow again.
What is happening, why set focus on VK_RIGHT stops widnow from getting messages?
I have Listbox. I want to press VK_RIGHt to move selection to another control(window - here I want it o be hEdit2(in future it will be probaly another ListBox)) But when I do it, cursor of selection moves down - so I added if(GetKeyState(VK_RIGHT)){} And it works but after changing focus to another control application freezes (UNTIL I PRESS ANOTHER TIME VK_RiGHT). wtf?
LRESULT SelIndex;
int rozmiar;
HWND hTmp;
// main message loop
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
// this always works
if ((HWND)msg.hwnd == hWndExit) { return 0; }
// this never tried I guess everything
if ((HWND)msg.hwnd == hWndList) {
SetForegroundWindow(hWndList); hTmp=SetFocus(hWndList); SendMessage(hWndList, WM_SETFOCUS, (WPARAM)hTmp, 0);
AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(GetAncestor(hWndList, GA_ROOT), NULL), TRUE);
SendMessage(hWndList, WM_UPDATEUISTATE, MAKEWPARAM(UIS_CLEAR, UISF_HIDEFOCUS), 0);
}
if (GetKeyState(VK_RIGHT)) {
SelIndex = SendMessage(hWndList, LB_GETCURSEL, 0, 0L);
rozmiar = (int)SendMessage(hWndList, LB_GETCOUNT, 0, 0);
// SendMessage(hEdit2, WM_SETFOCUS, (WPARAM)hWndList, 0);
// hTmp = SetFocus(hEdit2);
SetForegroundWindow(hEdit2);
if(SelIndex<rozmiar-1){ SendMessage(hWndList, LB_SETCURSEL, SelIndex-1, 0L); }
else { SendMessage(hWndList, LB_SETCURSEL, SelIndex, 0L); }
}
DispatchMessage(&msg);
}
}
/////////////////////////////////////////////////////////////////////////////////

Continue update&render whilst handling windowproc messages

I've created a win32 C++ app that uses direct2d to plot stuff in a window. In a loop as shown below, I call render and update whilst also peeking at any messages. But when ever I have WM_COMMAND that lets WindowProc call a MessageBox() or DiaLogBox(), my update&render loop no longer gets executed and the plot window freezes. Is there a simple fix with another loop structure or do I need to enter the, for me, unknown world of multithreading?
while (message.message != WM_QUIT)
{
if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
DispatchMessage(&message);
else
{
Update();
BeginDraw();
Render();
EndDraw();
}
}
EDIT: By popular demand I have changed above "ugly" mechanism in to another approach, yet with the same issue
bool runGame = true;
while (runGame)
{
while (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
DispatchMessage(&message);
if (message.message == WM_QUIT)
runGame = false;
}
Update();
BeginDraw();
Render();
EndDraw();
}

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();
}

Begin a Sprite C++ DirectX

Any idea why is this happening?
In my Main.cpp, I just called the function like this:
Character *Police;
Police = new Character;
Police->Initialize("Police.png", 50.0f, 50.0f, 64.0f, 63.5f);
MSG Msg;
while (GetMessage(&Msg, NULL, 0, 0))
{
while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
if (Msg.message == WM_QUIT) break;
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Inputs->GetInput();
Graphic->ClearBegin();
Police->Transform();
Police->Begin();
Police->Draw();
Police->End();
Graphic->EndPresent();
}
I dont get what is this exception is about..
Your Sprite pointer is not initialized. You see it on the bottom of your screenshot (Sprite is NULL).
Trying to call a function for a null objject results in an access violation.
Consider initializing your sprite with D3DXCreateSprite() before using it.

RegisterHotKey without a modifier in 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.