RegisterHotKey and WM_HOTKEY

This is a discussion on RegisterHotKey and WM_HOTKEY within the Windows Programming forums, part of the Platform Specific Boards category; Code: LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_CREATE: { HWND hWindow = ...

  1. #1
    Registered User CrissyCrisCris's Avatar
    Join Date
    Aug 2009
    Posts
    13

    RegisterHotKey and WM_HOTKEY

    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
        switch(Msg)
        {
    		case WM_CREATE:
    			{
    				HWND hWindow = FindWindow("Untitled Window Class", "Untitled Window Name");
    				if (hWindow == NULL)
    				{
    					MessageBox(hwnd, "Error", "", MB_ICONEXCLAMATION | MB_OK);
    					//PostMessage(hwnd, WM_QUIT, NULL, NULL);
    				}
    				RECT rWindow;
    					GetWindowRect(hwnd, &rWindow);
    				hEdit1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL, 0, 0, rWindow.right - rWindow.left - 5, rWindow.bottom - 150, hwnd, (HMENU)IDC_EDIT1_BOX, GetModuleHandle(NULL), NULL);
    				hEdit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL, 0, rWindow.bottom - 150, rWindow.right - rWindow.left - 5, 150, hwnd, (HMENU)IDC_EDIT2_BOX, GetModuleHandle(NULL), NULL);
    
    				RegisterHotKey(hEdit2, VK_RETURN, NULL, 2000);
    			}
    			break;
    		case WM_HOTKEY:
    			switch (LOWORD(wParam))
    			{
    				case 2000:
    				MessageBox(hwnd, "It Worked", "It Worked", MB_OK);
    				break;
    			}
                            break;
            case WM_DESTROY:
                PostQuitMessage(0);
    			return 0;
        }
        return DefWindowProc(hwnd, Msg, wParam, lParam);
    }
    I got the window setup the way I wanted, but when I use RegisterHotKey for the second textbox and tested it out, it didn't work. What did I do wrong? Or is there a better way to check when the user presses return/enter in the text boxes?

    Also In the text boxes, when I type to the bottom of the box it will auto scroll down, but it will is one 1 short of that...

    This is what it looks when when I use the auto scrolling.
    Code:
    Text
    Text
    Text
    -----------------End Of the Text Box-----------------
    End of Text
    Is there a way to fix that too?

  2. #2
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Windows specific question?

    By the way, you may use SetWindowsHookEx instead.
    Just GET it OFF out my mind!!

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    Moved to Windows Programming.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    RegisterHotKey sets a global hot key across the desktop. If you try to register a hot key that has already been registered once before it will not work. This also means RegisterHotKey() overrides any window that you may have open.

    Example: I have ctrl+O as a hotkey in one of my programs. When I open Word and hit ctrl+O Word no longer tries to open a file, but mine will try regardless of having focus or not.

    I'm going to take a stab and say you have an MDI. This is why it does not work for the second, third, fourth etc textbox.

  5. #5
    Registered User CrissyCrisCris's Avatar
    Join Date
    Aug 2009
    Posts
    13
    After server hours, I found a way to do it.

    Code:
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
    		if (Msg.message == WM_KEYDOWN && Msg.wParam == VK_RETURN)
    		{
    			if (GetFocus() == hEdit1)
    				MessageBox(hwnd, "It worked", It worked", MB_OK);
    		}
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,860
    Quote Originally Posted by CrissyCrisCris View Post
    Or is there a better way to check when the user presses return/enter in the text boxes?
    Add ES_WANTRETURN | ES_MULTILINE to get a msg when the user presses ENTER in an edit.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21