Click to See Complete Forum and Search --> : not working in win 98?
SteveCRM
Sep 18th, 2002, 04:38 PM
for some reason this works on xp but not on the schools 98 machines...any idea why?
#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 :confused:
CornedBee
Sep 18th, 2002, 05:00 PM
Win98 sucks, but so does WinXP...
I can't help you on that, but use <iostream> anyway...
SteveCRM
Sep 19th, 2002, 07:14 PM
from msdn:
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?
Zaei
Sep 19th, 2002, 09:05 PM
Use GetKeyState() =).
Z.
CornedBee
Sep 20th, 2002, 02:00 AM
GetKeyState is only for use within message handlers.
Use DirectInput :)
SteveCRM
Sep 20th, 2002, 03:02 PM
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 :confused:
Zaei
Sep 20th, 2002, 03:25 PM
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.
CornedBee
Sep 21st, 2002, 05:12 AM
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.
SteveCRM
Sep 21st, 2002, 07:07 AM
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];
}
}
Daok
Sep 21st, 2002, 01:20 PM
take off the &
SteveCRM
Sep 21st, 2002, 02:06 PM
cannot convert parameter 1 from 'char [256]' to 'unsigned char *'
parksie
Sep 21st, 2002, 02:10 PM
for(int i=0; i<257; i++)...should be 256 :)
Try using unsigned char for the array then.
SteveCRM
Sep 21st, 2002, 02:38 PM
could have sworn I already tried that
runs fine...but the outputs a little odd
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);
}
:confused:
Daok
Sep 21st, 2002, 03:57 PM
I cannot test it cause I got 2 errors from the winuse.h :(
SteveCRM
Sep 21st, 2002, 04:04 PM
?
just use windows.h
SteveCRM
Sep 21st, 2002, 04:09 PM
#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;
}
Daok
Sep 21st, 2002, 04:11 PM
That code compile but it not usefull cause it print only happy face :D
#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
Zaei
Sep 21st, 2002, 05:16 PM
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.
SteveCRM
Sep 21st, 2002, 05:48 PM
so like this???
#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;
}
:confused:
Zaei
Sep 21st, 2002, 05:58 PM
No, I believe:
if((ks[i]>>7)==1)
Z.
SteveCRM
Sep 21st, 2002, 06:22 PM
doesn't seem to really be working for me:
#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;
}
CornedBee
Sep 23rd, 2002, 03:24 AM
instead of
(ar[i] >> 7) == 1
try
(ar[i] & 0x80) != 0
where is the problem with your current code?
SteveCRM
Sep 23rd, 2002, 06:14 AM
I just can't get my program to output what keys im pressing...
#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 :confused: it just doesn't register anything in the if statement this way :(
Zaei
Sep 23rd, 2002, 06:49 AM
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.
CornedBee
Sep 24th, 2002, 05:07 AM
That could be true, maybe the console swallows the keys or something...
Use <iostream>, not <iostream.h>
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.