Skip to content

Commit

Permalink
Working Window
Browse files Browse the repository at this point in the history
It works like this, but some code review would be good.
  • Loading branch information
ZombieRoxtar committed Apr 1, 2015
1 parent 58d162c commit ab30c96
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Hotkey-Blocker/Hotkey-Blocker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,30 @@

#include <iostream>
#include <windows.h>
using namespace std;

/* This is our message callback */
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int main()
{
SetConsoleTitle( TEXT( "Hotkey Blocker" ));
/* Create window class */
WNDCLASSEX wc;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = WndProc;
wc.lpszClassName = TEXT("MessageWindowClass");
RegisterClassEx(&wc);

/* Create window (uses HWND_MESSAGE as the parent window to create message only window) */
HWND window = CreateWindow(LPCWSTR(wc.lpszClassName), LPCWSTR("HotkeyMesageWindow"), 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);

This comment has been minimized.

Copy link
@aroxby

aroxby Apr 1, 2015

Collaborator

The use of the LPCWSTR macro tells me you have your app set to unicode in the project properites. I think using 'multi-byte character set' or whatever the alternative is will make your life easier.

if(!window)
{
cerr<< "Can't create a window for messaging!" <<endl;
exit(1);
}

SetConsoleTitle(TEXT("Hotkey Blocker"));
bool keepGoing = true;

int cappedKeys[] =
Expand All @@ -19,7 +39,7 @@ int main()
VK_F4
};
const static int arraySize = sizeof(cappedKeys) / sizeof(int);
printf( "To exit press Num Pad Divide.\n" ); /* Why doesn't this get passed? */
printf( "To exit press Num Pad Divide.\n" ); /* Why doesn't this key get passed? */

bool errorKey = false;
for( int i = 0; i < arraySize; i++ )
Expand All @@ -33,6 +53,7 @@ int main()
if( !errorKey )
printf( "Ready to rock and roll!\n" );

/* Main message loop */
MSG msg = {0};
while (( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) != 0 ) || keepGoing )

This comment has been minimized.

Copy link
@aroxby

aroxby Apr 1, 2015

Collaborator

Looks like this loop logic will cause the same message to be processed repeatedly.
Let's assume that keepGoing is true and PeekMessage returns false.

Also, PLEASE move your WM_HOTKEY logic into your WndProc, that's what it's there for.

{
Expand Down

4 comments on commit ab30c96

@ZombieRoxtar
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The multi-byte thing is great. I'll have to come back to the rest.

@aroxby
Copy link
Collaborator

@aroxby aroxby commented on ab30c96 Apr 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can solve the first half of comment number 2 by either using a nested loop or:
Remove keepGoing from your while condition and insert if(!keepGoing) break; at the end of your loop.

Just a couple of ideas.

@ZombieRoxtar
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like your saying that I should move my message logic to the other function. Then main will be looped with a dumb while(keepGoing) sleep(); and keepGoing would have to be global?

@aroxby
Copy link
Collaborator

@aroxby aroxby commented on ab30c96 Apr 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No you still have things in the main loop. Yes, keepGoing will need to be global.

Something like:

do
{
    if(PeekMessage(...))
    {
        TranslateMessage(&msg);
        //This will call your WndProc
        DispatchMessage(&msg);
    }
    Sleep(0);
} while(keepGoing);

Please sign in to comment.