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.

Newcomers DBPro Corner / Best way to grab key strokes?

Author
Message
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 18th Jul 2003 23:44
My current game, Pegz3D, uses the keyboard arrow keys for navigation. However, I need to have it so when you tap the down arrow once, the game recognizes it as *one* stroke. When I run the game and tap the down arrow, it recognizes it as a bunch of down strokes, like 20.

Anyways, I fixed the problem by using a simple boolean keyPressed flag so the engine only executes one key at a time. Is there anything I'm missing here or is this normal?

I'm thinking it's normal because as humans it's impossible for us to press the key once in, say 40 milliseconds, which is my sync time. The game is simply following directions and recognizing I'm pressing the key over several syncs. Any other insights?

Thanks
http://www.automatongames.com/
StrikerBlueI
21
Years of Service
User Offline
Joined: 31st Jan 2003
Location: United States
Posted: 19th Jul 2003 00:08
I actualy had the same problem where i only wanted one keystroke at a time. I did make my own 'KeyPressed' function but no exactly like yours. The ENTRY$ function in DarkBasic is supposed to return all the keys pressed during a refresh cycle, where the data returned from ENTRY$ is not affected by the refresh rate of your application but when I used ENTRY$ it returned a long repetition of keystrokes as you were getting when you first wrote your program. So the only way to get one keystroke is to make your own function or boolean variable it seems.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 19th Jul 2003 00:24
That's pretty much the way you have to do it.

Another useful variation is the push-on,push-off keypress like the on/off button on a TV. That can be done with a single boolean flag too.
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 19th Jul 2003 01:48
Come to think about it, this is one of the reasons why I love DarkBasic's power over the "click and play", "no programming needed", "do everything for you" game tools. This keystrokes problem is not actually a problem per se-- it's behaving exactly the way it's told. Just shows how powerful real, logical programming (C++, C, Basic) is, in comparison to "fake" programming.

http://www.automatongames.com/
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 21st Jul 2003 17:07
This is something I have been pondering also. However,

[quote]I fixed the problem by using a simple boolean keyPressed flag

May sound simple to you but I have no idea what you're talking about

Any chance of a code snippet please?

Nine lives to choose from and I get stuck with this one!
ReD_eYe
21
Years of Service
User Offline
Joined: 9th Mar 2003
Location: United Kingdom
Posted: 21st Jul 2003 17:47
example of boolean keypressed flag

if spacekey()=1 and keypressed=0
keypressed=1
*do some kind of code
endif

if spacekey()=0 then keypressed=0

that works because after pressing the key it make the if statement false which means that code can't be executed again until the spacekey is released.(equals=0)

You know whats weird???
Donald duck never wore pants, yet everything time he got out of the shower he put a towel around his waist...
Whats with that???

Login to post a reply

Server time is: 2024-09-20 15:29:23
Your offset time is: 2024-09-20 15:29:23