Thread: Key press in windows

  1. #1
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79

    Key press in windows

    Is there really no other way of getting key press input than with WM_KEYDOWN and virtual keys? It's rather cumbersome to work with I think. Is there a third party solution around somewhere?

    I've always been using IUP, when building GUIs and they have made it pretty simple. All printable keys are mapped to their corresponding ascii value, and it's very easy to check if the control- or some other key is pressed at the same time.

    Code:
    switch (kp) {
    
        case K_A: // a
            break;
    
        case K_sA: // Shift + a = A
            break;
    
        case K_cF1: // Ctrl + F1
            break;
    
        case K_mF1: // Alt + F1
            break;
    
    }
    EDIT: Programming in C...
    Last edited by Fader_Berg; 02-07-2011 at 02:08 AM. Reason: Language manifest

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You should probably build accelerator tables...

    Using Keyboard Accelerators (Windows)

    Basically you build an accelerator table in your resource editor. Then you add TranslateAccelerator() to your message dispatcher and use LoadAccelerator() to load the table for it. From there on every mapped keystroke is translated into a WM_COMMAND message... with a bit of cleverness you can make these the same values as your buttons and menus emit, making a keypress no different than clicking a button...

    If you still insist on doing it the hard way you can add TranslateMessage() to your message dispatcher and handle WM_CHAR messages... These carry the ASCII values...
    Last edited by CommonTater; 02-07-2011 at 07:43 AM.

  3. #3
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    I don't know if this will help, but maybe...

    Code:
    static int keyCache[128];
    
    void KeysInit( void )
    {
    	int i;
    	for ( i = 0; i < 128; i++ )
    		keyCache[i] = 0;
    }
    
    int is_key_down(char vk)
    {	
    	return !(GetAsyncKeyState(vk) & 0x8000) ? keyCache[vk] = 0 : 1;
    }
    
    int was_key_pressed(char vk)
    {
    	if(is_key_down(vk))
    	{
    		if(!keyCache[vk])
    			return keyCache[vk] = 1;
    
    		return 0;
    	}
    	return keyCache[vk] = 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help: wait for key press
    By Pan in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2006, 12:26 AM
  2. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  3. Windows Key Unstopable?!
    By Geolingo in forum Windows Programming
    Replies: 4
    Last Post: 09-15-2003, 02:28 AM
  4. annoying little windows key
    By dP munky in forum Tech Board
    Replies: 3
    Last Post: 03-08-2003, 11:11 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM