Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keyboard input - multiple keys?
#1
In the game that I am currently creating, I want to add a two-player mode. This would require being able to handle the input of two keys at the same time. I know this can be done with Shift, Ctrl, etc., but is it possible, in Qbasic, to be able to detect 2 or more keys at the same time/ in the same loop? (i.e.: Player one holds down the left arrow key, while player two holds down key 'D')

Thanks to anyone who helps me out!
img]http://b.domaindlx.com/cygh/mycophob3.gif[/img]
Reply
#2
You need a keyboard handler. Look for Milo Sedlacek's.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#3
Check the code post for a multikey handler in pure qb, i used it. Milo's blows it out of the water as it is in asm, but for small tasks the one at the code post is great.
b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
Reply
#4
...but also bear in mind that just how many keys can be read at once will depend on your keyboard. mine can handle about 5 - which might sound a lot, but think if I was playing a 2-player game were each player had 4 direction keys and a fire button.. both move diagnally and try to fire at same time.. bzzzzt!

i had to ditch a 3 player mode in one of my games because of that. also, some key combinations seem to work better than others.
In a world without walls and doors, who needs Windows and Gates?
Reply
#5
I can't seem to find anything on this site about a multikey handler... what section/forum/subject is it in?
img]http://b.domaindlx.com/cygh/mycophob3.gif[/img]
Reply
#6
it's not on this site, this site isnt great as a download resource. QB45's better for that.

QB45 Link
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#7
The old ABC is still userful...
http://www.basicguru.com/files/abc/abc9707/multikey.bas
Antoni
Reply
#8
oh yeah, forgot about those good ol' packets.
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#9
Okay, I looked at that multikey handler and understood enough of it to make my own. However, both that one and my own have one small problem - I'll explain a little bit of how a pure QB multikey handler works, in case you don't know, and then I'll explain what the problem is.

Basically, the multikey handler is put in the program's main loop; It then checks for keyboard input, and if it finds a certain key is pressed, it changes a corresponding array value to a number such as 1. Also, the multikey handler checks for keys that have just been un-pressed, and changes the corresponding array value to zero.

Here's a simple multikey handler; when escape is pressed, the program ends.

Code:
DECLARE FUNCTION Multikey (keynum)
DIM SHARED KeyInput(255)

DO
Esc = Multikey(1)   '1 is the keycode for escape
IF Esc = 1 THEN END   'If Multikey returns 1, then it is pressed
LOOP

FUNCTION Multikey (keynum)

KeyInp = INP(96)   'Check for keyboard input
IF KeyInp <= 127 THEN   'Check for pressed keys
KeyInput(KeyInp) = 1
END IF
IF KeyInp >= 128 THEN   'Check for unpressed keys
KeyInput(KeyInp - 128) = 0
END IF
Multikey = KeyInput(keynum)   'Return value

END FUNCTION

This small bit of code works pretty slick - except for when another key is pressed before the handler detects the un-press of a different key.

Since the handler didn't detect the un-press of that key, the program acts as if that key is still down.

Simply optimizing the code for faster execution isn't the answer; the handler still occasionally misses an un-press. I've also tried running the function several times in the loop, but the problem still exists.

If anyone can find out a solution to this, then congratulations, because I sure can't!
img]http://b.domaindlx.com/cygh/mycophob3.gif[/img]
Reply
#10
It works under WinXP too?
Because some key handlers freeze it up!

I wrote the same thing under TC, I'm using it in my game since November. But I only experienced it just ONE TIME, and I played with my game for many hours since that! (More than 200 Smile

So I think this is a good code! And mine can work well with NT Windowses too Smile (But it's Turbo - C)
ingCheetah, the Running Cheetah Software programmer

http://rcs.fateback.com/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)