Results 1 to 25 of 25

Thread: not working in win 98?

  1. #1

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

    not working in win 98?

    for some reason this works on xp but not on the schools 98 machines...any idea why?

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int GetKey();
    
    int main()
    {
    	int test;
    	while(1)
    	{
    		test = GetKey();
    		if(test!=256)
    			cout<<test<<endl;
    	}
    	
        return 0;
    }
    
    int GetKey()
    {
    	int num = 0;
    
    	for(int i=0; i<256; i++)
    	{
    		num = GetAsyncKeyState(i);
    		if(num!=0)
    		{
    			return i;
    		}
    	}
    }
    it says when the mouse is being clicked, but nothign works for the keyboard

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Win98 sucks, but so does WinXP...

    I can't help you on that, but use <iostream> anyway...
    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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    from msdn:
    Code:
    Return Value
    
    If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks. 
    
    Windows NT/2000/XP: The return value is zero for the following cases: 
    
    The current desktop is not the active desktop
    The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.
    Windows 95/98/Me: The return value is the global asynchronous key state for each virtual key. The system does not check which thread has the keyboard focus.
    
    Windows 95/98/Me: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.
    I don't really understand if thats a problem but does that mean it will work differently in 98?

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Use GetKeyState() =).

    Z.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    GetKeyState is only for use within message handlers.

    Use DirectInput
    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    don't have directx on the school computers

    how about GetKeyboardState()? that seems like it will work...but whenever I try it I get a conversion error...haven't been able to fix it

  7. #7
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by CornedBee
    GetKeyState is only for use within message handlers.

    Use DirectInput
    DI is even worse... then you actually have to process the keyboard state =).

    Z.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    At least it represents the REAL state of the keyboard...

    GetKeyboardState should work. It expects an array of chars to be passed!

    GetKeyState returns the state of a key at the time of the last message - which is a little bit tricky in a console app.
    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.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    I tried that but I get this error

    cannot convert parameter 1 from 'char (*)[256]' to 'unsigned char *'


    char ks[256];
    while(1)
    {
    GetKeyboardState(&ks);
    for(int i=0; i<257; i++)
    {
    cout<<ks[i];
    }
    }

  10. #10
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    take off the &

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    cannot convert parameter 1 from 'char [256]' to 'unsigned char *'

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
     for(int i=0; i<257; i++)
    ...should be 256

    Try using unsigned char for the array then.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    could have sworn I already tried that

    runs fine...but the outputs a little odd

    Code:
    unsigned char ks[256];
    	for(int i=0;i<256; i++)
    		ks[i] = NULL;
    	while(1)
    	{
    		GetKeyboardState(ks);
    		for(i=0; i<256; i++)
    			cout<<ks[i];
    		cout<<endl<<endl;
    		Sleep(1000);
    	}

  14. #14
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I cannot test it cause I got 2 errors from the winuse.h

  15. #15

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

    just use windows.h

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int main()
    {
    	unsigned char ks[256];
    	for(int i=0;i<256; i++)
    		ks[i] = NULL;
    	while(1)
    	{
    		GetKeyboardState(ks);
    		for(i=0; i<256; i++)
    			cout<<ks[i];
    		cout<<endl<<endl;
    		Sleep(1000);
    	}
    	
    	return 0;
    }

  17. #17
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    That code compile but it not usefull cause it print only happy face

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    
    void main()
    {
    	unsigned char *ks = new unsigned char[255];
    
    	while(true)
    	{
    		for (int i = 0 ; i < 255 ; i++)
    		{
    			if(GetKeyboardState(ks) !=0)
    				cout << (unsigned char)ks[i];
    			else
    				cout << "Error : Function GetKeyboardState fail\n";
    		}//Fin du for : i
    	}//End while
    }//Fin du main

  18. #18
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Gotta read the function specs in the MSDN, guys =):
    When the function returns, each member of the array pointed to by the lpKeyState parameter contains status data for a virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the caps lock key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
    Z.

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    so like this???

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int main()
    {
    	unsigned char ks[256];
    	for(int i=0;i<256; i++)
    		ks[i] = NULL;
    	while(1)
    	{
    		GetKeyboardState(ks);
    		for(i=0; i<256; i++)
    		{
    			if(HIWORD(ks[i])==1)
    				cout<<i<<" ";
    		}
    		cout<<endl<<endl;
    	}
    	
    	return 0;
    }

  20. #20
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    No, I believe:
    Code:
    if((ks[i]>>7)==1)
    Z.

  21. #21

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    doesn't seem to really be working for me:

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int main()
    {
    	unsigned char ks[256];
    	for(int i=0;i<256; i++)
    		ks[i] = NULL;
    	while(1)
    	{
    		GetKeyboardState(ks);
    		for(i=0; i<256; i++)
    		{
    			if((ks[i]>>7)==1)
    				cout<<i<<" ";
    		}
    		cout<<endl<<endl;
    	}
    	
    	return 0;
    }

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    instead of
    (ar[i] >> 7) == 1
    try
    (ar[i] & 0x80) != 0

    where is the problem with your current code?
    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.

  23. #23

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    I just can't get my program to output what keys im pressing...
    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int main()
    {
    	unsigned char ks[256];
    	bool test = true;
    	while(test == true)
    	{
    		GetKeyboardState(ks);
    		for(int i=0; i<256; i++)
    		{
    			if((ks[i] & 0x80) != 0)
    			{
    				cout<<i<<" ";
    				test = false;
    			}
    		}
    	}
    	
    	return 0;
    }
    this doesn't seem to be working either it just doesn't register anything in the if statement this way
    Last edited by SteveCRM; Sep 23rd, 2002 at 06:17 AM.

  24. #24
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    This is annoying... Id like to put it down to this being a console application. I use almost exactly the same code for processing my DInput keyboard buffer, and it works like a charm.

    Z.

  25. #25
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That could be true, maybe the console swallows the keys or something...

    Use <iostream>, not <iostream.h>
    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.

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