Results 1 to 17 of 17

Thread: SHOULD be simple

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    SHOULD be simple

    If you remember I was trying to make a keyboard/mouse trapper for school. Next week I need a prototype but my code isn't working for some reason.

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    	unsigned char ks[256];
    	int num;
    	while(1)
    	{
    		for(int i=0; i<256; i++)
    		{
    			num = GetAsyncKeyState(i);
    			if(num!=0)
    			{
    				cout<<i<<" ";
    			}
    		}
    	}
    	
    	return 0;
    }
    it should give the ascii value of keys when pressed

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    how about something like:
    Code:
    while(1){
         sleep(50);                      // nobody types this fast
         if(GetAsyncKeyState(27)) break; // exit on esc
         for(int i=32;!GetAsyncKeyState(i)&& i<128;i++);
         if(i<128) cout<<i<< " "; 
    }

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    nothing

    escape doesn't even work...and no mouse, the mouse worked before too

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    GetAsyncKeyState requires a hexadecimal value. For example, 0x27 instead of just 27....
    Baaaaaaaaah

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    I forgot - Abdul's right it uses short int or char
    Code:
    for(char i=32;!GetAsyncKeyState(i)&& i<128;i++);

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    not working abdul

    for some reason the only key this works with is the windows keys (91 and 92)

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    	unsigned char ks[256];
    	int num;
    	while(1)
    	{
    		for(int i=0; i<256; i++)
    		{
    			num = GetAsyncKeyState(i);
    			if(num!=0)
    			{
    				cout<<i<<" ";
    			}
    		}
    	}
    	
    	return 0;
    }

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    would it be easier to use GetKeyboardState()?

  8. #8
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    You code works fine for me Steve. I remember there is some OS related stuff with GetAsyncKeyState() function. I'm using Windows 2000.
    Baaaaaaaaah

  9. #9
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Use GetAsyncKeyState() if you want more than 1 key detected at the same time, else don't use it.

    [edited] erroneous info given

    Alternately, look it up in the MSDN.
    Last edited by transcendental; Oct 4th, 2002 at 09:46 PM.

  10. #10
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Use GetAsyncKeyState() if you want more than 1 key detected at the same time, else don't use it.
    GetAsyncKeyState() can only check for 1 key provided as a parameter to the function. It doesn't have to wait for the key or something (hence the short word "Async") so it returns with the status of the key depending on what's in the message queue.
    Maybe you meant GetKeyboardState()?

    EDITED: Information deleted from the above post.
    Baaaaaaaaah

  11. #11
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    I mean GetAsyncKeyState() but I never said it can detect multiple keys. I only said,

    "Use GetAsyncKeyState() if you want more than 1 key detected at the same time, else don't use it."

    What I mean, is

    Code:
    if (GetAsyncKeyState(VK_SPACE)
    {
    //do what you want here
    }
    
    if (GetAsyncKeyState(VK_RETURN)
    {
    //Do what you want here
    }
    
    ...And so on
    Not

    "GetAsyncKeyState() can detect multiple keys."

    ============================================
    I wondered if you modified Steve's code. There is no breaking out of the while loop.

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by transcendental
    I mean GetAsyncKeyState() but I never said it can detect multiple keys. I only said,

    "Use GetAsyncKeyState() if you want more than 1 key detected at the same time, else don't use it."
    I always thought more than 1 meant multiple.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The Async in GetAsyncKeyState stems from it's difference to GetKeyState. GetKeyState retrieves only the status of the keys at the time the last message was retrieved from the queue, so it will not change within one handler, no matter how often you call it.
    Bad code:
    while(1) {
    if(GetKeyState(VK_ESCAPE) & 0x80) break;
    }
    This loop will run forever.

    GetAsyncKeyState on the contrary retrieves the state of the key as it is NOW, asynchronously from the messages, hence the Async.

    Both messages however want not normal ASCII code but rather the virtual key codes that are defined as VK_ constants. VK_A to VK_Z "happen" to be the same as the ASCII values of 'A' to 'Z'.
    Both shouldn't be tested against 0, but rather the result bitwise ANDed with 0x80 ( & 0x80) should be tested against 0.
    Just because I feel like doing this: In ASM this would look like
    Code:
    push VK_ESCAPE ; whatever value
    call GetAsyncKeyState
    and eax, 80h
    jnz KeyWasPressed
    I think trans misunderstood the meaning of Async, this is why his post is wrong (it is).

    Abdul: hexadecimal or decomal values make no difference to the cpu, it only uses binary...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    so do you think it would be better to use GetKeyboardState() instead?

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Probably. I don't know, I've never tried to do such a thing.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    I found out GetAsyncKeyState() would not work in a console, no matter how hard I tried. Probably whatever STL iostream screw it up.

    It works with a little modification, for a Win32 Windows app. Here's the code,

    PHP Code:
        // Main message loop:
        
    while (TRUE
        {
            if (
    PeekMessage(&msg,NULL,0,0,PM_REMOVE))
            {
                if (
    msg.message == WM_QUIT) break;
                
    //if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
                
                    
    TranslateMessage(&msg);
                    
    DispatchMessage(&msg);
            }
            else
            {
                
    bool quitnow=false;
                for(
    int i=0i<256i++)
                {
                    
    SHORT key=GetAsyncKeyState(i);
                    if(
    key 0x8000)
                    {
                        
    char str[50]="Number of the Keypress";
                        
    char buf[10];
                        
    itoa(i,buf,10);
                        
    strcat(str,buf);
                        
    MessageBox(hWnd,str,"Numbers",MB_OK MB_APPLMODAL);
                        if(
    i==VK_SPACE)//press space_bar to quit
                          
    quitnow=true;
                    }
                }
                if (
    quitnow==true)
                break;
            }
        } 
    However, when I press shift keys, only VK_SHIFT is detected, no VK_LSHIFT and VK_RSHIFT keys. Maybe it is because VK_SHIFT (0x10) comes before (0xA0) and (0xA1). I do not know.

    So be very careful if you intend to use the virtual keys it generates. Better stick with the MSDN docs, IMO.

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    so then GetKeyboardState() is definately the way to go it sounds like thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width