Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Key press and release

Author
Message
Cagedrei
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 8th Apr 2009 01:52
I needed to track key press and release events, to control my player animations. I couldn't find any really extensible examples at the forum here, so I put this together.

Please offer suggestions or better solutions, if you have them. DBP only allows one to check two of the four key states, by default, so this seems to be a fairly common problem.

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Apr 2009 02:03 Edited at: 9th Apr 2009 02:04
Quote: "Please offer suggestions or better solutions, if you have them"

You could use the win api and GetAsyncKeyState to determine the keystate. The high order bit determines if the key is pressed. The low order bit determines if it was pressed previously.

Though this example is a bit clunky, it shows the idea. Written for DBC, change & to && for Pro. It is only checking for the 'A' key. You would change that by using the virtual key codes.



Enjoy your day.
Cagedrei
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 9th Apr 2009 05:58
Ooh. Interesting. In searching the forums for a solution, I'd never seen anything about this possibility.

What you're doing is loading one of the Windows dlls directly? Or is the user23.dll something I'd need to track down for DBP? I'd assumed this problem would have to be approached directly, using Dark Basic's commands. If there are better tools, I'm pleased to know it.

I'll see what I can learn about the win32.dll. Thanks, Latch.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Apr 2009 17:47
Quote: "What you're doing is loading one of the Windows dlls directly?"

Yes.
The whole programming toolset is called the win32 API. There are a series of DLLs that are part of the windows operating system. Inside the DLLS are all kinds of functions. The DLL I used was User32.dll which should be in all windows distributions. The function in the DLL was GetAsyncKeyState.

Quote: "I'd assumed this problem would have to be approached directly, using Dark Basic's commands. If there are better tools, I'm pleased to know it."

I don't know if it is a "better" method per se, but it is less code. The results are the 4 states you are looking for I think - key IS down, key IS up, key WAS down, key WAS up. You'll just have to supply the virtual key code for the key you want to check (can be mouse buttons as well). Virtual Key Codes - these are in hex.

If the result is < 0 then then the key is pressed
If the result AND (&&) 1 is 1 the key was pressed before it's current state
If the result && 1 is 0 then the key was up before it's current state
If the result is 0 then the key is up now and it wasn't pressed before it's current state

Enjoy your day.
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 11th Apr 2009 19:17
this is what i use for keeping track of keystates and keyclicks.
You will find it useful im sure.



There is always one more imbecile than you counted on.
Cagedrei
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 16th Apr 2009 08:39
Thanks, CuCuMBeR. I'll try it. Sorry for the delayed response. I hadn't checked this forum for a few days, and I forgot to check the mail response feature when I posted.

Login to post a reply

Server time is: 2024-05-03 11:27:11
Your offset time is: 2024-05-03 11:27:11