Struggling with key state alternative

hi,

currently using this which works however causes some problems so i want alternative to getkeystate or getasynckeystate but follow same principle.

1
2
3
4
5
6
7
8
if(GetAsyncKeyState(vars.KeyToPress) < 0)
		{
			PerformAction();
		}
			Sleep(5);
		}
	return 0;
}


ideally something like mouse_event could work but this gives a bool type error;

1
2
3
4
5
6
7
8
if(mouse_event(vars.KeyToPress) < 0)
		{
			PerformAction();
		}
			Sleep(5);
		}
	return 0;
}


within my vars i would enter MOUSEEVENTF_LEFTUP for example.

i have also tried mapvirtualkey but cannot get it working in this scenario.

please provide suggestions not involving getkeystate or getasynckeystate, these work but i need the alternative.

thanks.
GetAsyncKeyState(vars.KeyToPress) < 0

For starters... <0 is the wrong check.

GetAsyncKeyState returns the real-time state of the key in bit 15, and the "key was just pressed now" state in bit 0. So if you want to check for one of those:

1
2
3
4
5
6
7
8
9
10
if(GetAsyncKeyState( key ) & 0x0001 )
{
    // key just pressed now (ie:  transitioned from being not pressed to being
    //   pressed)
}

if(GetAsyncKeyState( key ) & 0x8000 )
{
    // key is currently being held down
}



Other than that....
however causes some problems


Can you be more specific as to what the problems are?
hi, <0 simply states if it is an active key. what you wrote is simply not required.

i asked for an alternative to getasynckeystate / getkeystate based on my attempted examples not for more questions on why i dont want to use getasynckeystate.
i asked for an alternative to getasynckeystate / getkeystate based on my attempted examples not for more questions on why i dont want to use getasynckeystate.


*shrug* Fine.

I thought if I could solve your problem with GetAsyncKeyState, you would not have to search for an alternative. But apparently you're not interested.

I can't imagine any other WinAPI call is going to get around whatever problem you're having with GetAsyncKeyState (since they're all going to work the same way)... and since you won't tell me what's broken, it is difficult for me to recommend something I know will work for you. So all I can do it mindlessly list alternative methods of keyboard input and let you drudge through them all.

So here you go. Here's a couple ways I know of:

- WinAPI: GetKeyboardState

- WinAPI: Look for and handle WM_KEYDOWN and WM_KEYUP messages

- XInput: XInputGetKeyStroke

- DirectInput (old): CreateDevice (to create the keyboard object), then GetDeviceState (to get the state of the keys)

- 3rd party libs like SDL/SFML/wxWidgets/Qt/Allegro/FLTK

- conio.h: kbhit and getch

hi, <0 simply states if it is an active key. what you wrote is simply not required.


I suppose you're right... either will work since if the high bit is set, the resulting number will also be negative.

Though personally I find the & 0x8000 approach to be more explicit and easier to understand (since it's actually checking the desired bit... rather than assuming the desired bit causes the sign of the returned value to change).

But whatever floats your boat.
Last edited on
hi, its not that it doesn't work. it works perfectly but for one reason or another i need an alternative api call that gasks or gks. apologies for my previous reply. can you possibly do some pseudo examples?

p.s. this will be mouse1,2,3,4,5 that i will be using.
Last edited on
Topic archived. No new replies allowed.