Thread: key pressed

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    key pressed

    how can i check if 'Ctrl+A' pressed?? WM_HOTKEY??

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    WM_HOTKEY??
    Yep, that's right

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    ok,but how i check if ctrl+a i pressed??what i need to write in the if sentence??

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Think they are called keyboard accelerators, there are functions to add them from resource files and probably other ways too.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by hareldo View Post
    ok,but how i check if ctrl+a i pressed??what i need to write in the if sentence??
    Code:
    #pragma comment( lib, "user32.lib" ) 
    #include <windows.h>
    
    #define VK_A    0x041
    
    #define CALL_CMD_CTRL_A 101
    
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    char szClassName[ ] = "Hotkey Example";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    {
        HWND hwnd;               
        MSG messages;            
        WNDCLASSEX wincl;        
    
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;    
        wincl.style = CS_DBLCLKS;     
        wincl.cbSize = sizeof (WNDCLASSEX);
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                
        wincl.cbClsExtra = 0;                      
        wincl.cbWndExtra = 0;                      
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        if (!RegisterClassEx (&wincl))
            return 0;
        hwnd = CreateWindowEx (
            0,                   
            szClassName,         
            "My Hot Key example to catch Ctrl-A",       
            WS_OVERLAPPEDWINDOW, 
            CW_USEDEFAULT,       
            CW_USEDEFAULT,       
            544,                 
            375,                 
            HWND_DESKTOP,        
            NULL,                
            hThisInstance,       
            NULL                 
            );
        RegisterHotKey(hwnd, CALL_CMD_CTRL_A, MOD_CONTROL, VK_A);
        ShowWindow(hwnd, nFunsterStil);
        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                 
        {
            case WM_HOTKEY:
                switch(wParam)
                {
                    case CALL_CMD_CTRL_A:
                        MessageBox(NULL, "Got Ctrl-A", "We Got Ctrl-A", MB_OK);
                        break;
                }
                break;
            case WM_DESTROY:
                PostQuitMessage (0);       
                break;
            default:                      
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    BobS0327 is mighty kind. I would have probably just posted:

    Code:
    LRESULT CALLBACK callback(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
      if(msg == WM_HOTKEY)
      {
       if(wParam == whatever)
       {
         MessageBeep(MB_OK);
         return 0;
       }
      }
      return DefWindowProc (hwnd, message, wParam, lParam);
    }
    Kudos to you for not being quite so lazy.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by master5001 View Post
    BobS0327 is mighty kind. I would have probably just posted:

    Code:
    LRESULT CALLBACK callback(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
      if(msg == WM_HOTKEY)
      {
       if(wParam == whatever)
       {
         MessageBeep(MB_OK);
         return 0;
       }
      }
      return DefWindowProc (hwnd, message, wParam, lParam);
    }

    Kudos to you for not being quite so lazy.
    Actually, your way is the much preferred way than my switch statement which is just pure laziness on my part. Unfortunately, the majority of my posted examples come from copy and pasting (hacking) from my old projects and I don't devote enough time to cleaning them up.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Lol fair enough. I rarely copy and paste code anymore merely because it requires too much effort to find the exact code I am thinking of. Which has the added disadvantage of occassionally posting uncompileable code or just making simple mistakes. The other regulars here are always so kind and observant and love to remind us of our shortcomings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  4. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM