Keyboard probs

This is a discussion on Keyboard probs within the Windows Programming forums, part of the Platform Specific Boards category; I have this weird problem with keyboard checking. Neither the GetKeyState() function works nor can i use the WM_KEYDOWN message. ...

  1. #1
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408

    Keyboard probs

    I have this weird problem with keyboard checking.
    Neither the GetKeyState() function works nor can i use the WM_KEYDOWN message.
    What can be wrong?
    Are there any libraries or something that i need to include???

  2. #2
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    hmm, thats odd can you post your code/errors please
    TNT
    You Can Stop Me, But You Cant Stop Us All

  3. #3
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Im not getting any errors. But when i click a key nothing happens.

    I just did a WM_KEYDOWN case in my dialog processor with a messagebox as the only code in it.

    But nothing happens....

    Code:
        case WM_KEYDOWN:
        	MessageBox(hwndDlg, "Go shopping", "Shops", MB_OK);
    	return TRUE;
        break;

  4. #4
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hmm very strange, that code is correct and works fine with me, so i recon all that it can be is a problem with the handle to your dlg...

    So i think you need to do some checking for instance replace the msgbox with MessageBeep(0); or somthing else and see if you can get that to execute when you press a key. If it does then i think theres somthing wrong with your handle to the dialog. If it still dont work then i dont know , If it works then use FindWindow() with your applications details and it will return a hopefully working handle. I would also to advise you to do some error checking:

    Code:
    if(hWnd != NULL)
    {
    //do your code
    }
    
    else
    {
    //do somthing else
    }

    I hope that helps,
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  5. #5
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Hmm i tried making another program with a real registered window class instead of just a dialog box. And that worked fine.

    Heres the complete prog:

    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "sunrsc.h"
    
    
    int hp;
    char name[50];
    char race[50];
    int gc;
    int Str = 0;
    char SString[2];
    int Cha = 0;
    char CString[2];
    int Dex = 0;
    char DString[2];
    int Int = 0;
    char IString[2];
    int ppool = 12;
    char PString[3];
    int exswitch;
    int exrand;
    bool keys[256];
    HWND dlgHWnd;
    
    
    BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg, WPARAM wParam, LPARAM lParam) {
        switch (uMsg)
    	{
        case WM_INITDIALOG : HWND EditHandle;
                             SetDlgItemText(hwndDlg, ID_NAME, name);
                             SetDlgItemText(hwndDlg, ID_RACE, race);
                             sprintf(PString, "%i", ppool);
                             SetDlgItemText(hwndDlg, ID_CPPOOL, PString);
                             EditHandle = GetDlgItem(hwndDlg, ID_NAME);
                             SetFocus(EditHandle);
                             SendMessage(EditHandle, EM_SETSEL, 0,-1);
                             return 0;
                             break;
    	case WM_CLOSE:
    		EndDialog(hwndDlg, 0);
    		return TRUE;
        break;
    	case WM_COMMAND:
    		return MainDialog_OnCommand(hwndDlg, LOWORD(wParam), HIWORD(wParam), (HWND)lParam);
        }
        switch(wParam) {
        case VK_UP:
    		EndDialog(hwndDlg, 0);
        }
    	return FALSE;
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    
    {
    
    DialogBox(hInstance, MAKEINTRESOURCE(400), dlgHWnd, DialogProc);
    return 0;
    }
    EDIT:: Removed some unnecessary code (Command Proc)
    Last edited by ErionD; 04-20-2002 at 08:50 AM.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    dlgHWnd is never initialised to a value or given one in the code.
    But is also a param in the callback ie global and local in the callback function.

    DialogBox(hInstance, MAKEINTRESOURCE(400), dlgHWnd, DialogProc);

    should be
    iKill=DialogBox(hInstance, MAKEINTRESOURCE(400), NULL, DialogProc);//there is no parent to this window

    WM_KEYUP is a msg in itself. Testing all unprocessed msg's for one with the up arrow pressed is not the best code. Not to mention you have missed the 'break' after the only case in the switch.
    "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

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  2. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 03:46 PM
  3. Send output to keyboard buffer
    By DReynolds in forum C Programming
    Replies: 2
    Last Post: 06-06-2007, 04:44 PM
  4. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 04:34 PM

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