#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Disable key press


    its not exactly how it sound.
    i'm cheking if f1 is pressed with GetAsyncKeyState(VK_F1)
    there is a way, after key is pressed, to set the press to zero? i mean if i make while that perm check for key pressed, after i press f1 it set f1 to non pressed?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    there is a way, after key is pressed, to set the press to zero? i mean if i make while that perm check for key pressed, after i press f1 it set f1 to non pressed?
    You're using the wrong Win32 function. GetAsyncKeyState is essentially a keyboard polling method. What you need to use is a Windows hook mechanism. A Windows hook is mechanism that can be used to intercept events before they reach an application such as the F1 keyboard input.

    But anyway, here is a code sample for using GetAsyncKeyState.

    Code:
    #pragma comment( lib, "user32.lib" )
    #pragma comment( lib, "gdi32.lib" )
     
    #include <windows.h> 
     
    #define VK_F1 0x70
    #define WINDOW_CLASS_NAME "VIRTUALKEYDEMO"
     
    LRESULT CALLBACK WindowProc(HWND hwnd,
        UINT msg,
        WPARAM wparam,
        LPARAM lparam)
    {
        HDC     hdc;
        switch(msg)
        {  
            case WM_DESTROY:
                {
                    PostQuitMessage(0);
                    return(0);
                }
                break;
            default:
                break;
        }
        return (DefWindowProc(hwnd, msg, wparam, lparam));
    }
     
    int WINAPI WinMain( HINSTANCE hinstance,
                        HINSTANCE hprevinstance,
                        LPSTR lpcmdline,
                        int ncmdshow)
    {
     
        WNDCLASSEX winclass;
        HWND       hwnd;    
        MSG        msg;     
        HDC        hdc;     
     
        winclass.cbSize         = sizeof(WNDCLASSEX);
        winclass.style          = CS_DBLCLKS | CS_OWNDC |
            CS_HREDRAW | CS_VREDRAW;
        winclass.lpfnWndProc    = WindowProc;
        winclass.cbClsExtra     = 0;
        winclass.cbWndExtra     = 0;
        winclass.hInstance      = hinstance;
        winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
        winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
        winclass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
        winclass.lpszMenuName   = NULL;
        winclass.lpszClassName  = WINDOW_CLASS_NAME;
        winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
     
        if (!RegisterClassEx(&winclass))
            return(0);
     
        if (!(hwnd = CreateWindowEx(NULL,                
            WINDOW_CLASS_NAME,    
            "Virtual Key Demo for press and release of F1 key",
            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            0,0,    
            400,100, 
            NULL,   
            NULL,   
            hinstance,
            NULL)))
            return(0);
        while(TRUE)
        {
            if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
            {
                if (msg.message == WM_QUIT)
                    break;
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            hdc = GetDC(hwnd);
            if(GetAsyncKeyState(VK_F1) & 0x8000)
                TextOut(hdc, 0,16, "F1 key: Depressed", 18);
            else
                TextOut(hdc, 0,16, "                              ", 30);
            ReleaseDC(hwnd, hdc);
        }
        return(msg.wParam);
    }

IMN logo majestic logo threadwatch logo seochat tools logo