|
-
Oct 4th, 2002, 04:37 PM
#1
Thread Starter
Frenzied Member
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
-
Oct 4th, 2002, 04:49 PM
#2
Frenzied Member
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<< " ";
}
-
Oct 4th, 2002, 05:05 PM
#3
Thread Starter
Frenzied Member
nothing
escape doesn't even work...and no mouse, the mouse worked before too
-
Oct 4th, 2002, 07:38 PM
#4
PowerPoster
GetAsyncKeyState requires a hexadecimal value. For example, 0x27 instead of just 27....
-
Oct 4th, 2002, 07:43 PM
#5
Frenzied Member
I forgot - Abdul's right it uses short int or char
Code:
for(char i=32;!GetAsyncKeyState(i)&& i<128;i++);
-
Oct 4th, 2002, 07:45 PM
#6
Thread Starter
Frenzied Member
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;
}
-
Oct 4th, 2002, 07:53 PM
#7
Thread Starter
Frenzied Member
would it be easier to use GetKeyboardState()?
-
Oct 4th, 2002, 09:36 PM
#8
PowerPoster
You code works fine for me Steve. I remember there is some OS related stuff with GetAsyncKeyState() function. I'm using Windows 2000.
-
Oct 4th, 2002, 09:39 PM
#9
Hyperactive Member
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.
-
Oct 4th, 2002, 09:52 PM
#10
PowerPoster
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.
-
Oct 4th, 2002, 10:08 PM
#11
Hyperactive Member
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.
-
Oct 4th, 2002, 10:13 PM
#12
Stuck in the 80s
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.
-
Oct 5th, 2002, 03:18 PM
#13
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.
-
Oct 5th, 2002, 04:25 PM
#14
Thread Starter
Frenzied Member
so do you think it would be better to use GetKeyboardState() instead?
-
Oct 6th, 2002, 04:07 AM
#15
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.
-
Oct 6th, 2002, 08:41 AM
#16
Hyperactive Member
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=0; i<256; i++)
{
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.
-
Oct 6th, 2002, 03:27 PM
#17
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|