Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple keyboard inputs?

Status
Not open for further replies.

sandmann999

Programmer
Apr 28, 2001
52
US
I'm writing a program that would highly benefit from the use of numerous keyboard inputs at one time. Such as holding down "y" and "x" at the same time, or maybe the up arrow and the right arrow at the same time (to get a diagonal without using the numeric keypad.) It would be very useful for shortcuts or some of the games I've made that would love to become two player. ;)
Thanks for any help!
 
To use two keyboards you will probably have to use the COM statement, but to have two keystokes at once, I believe that the KEY statement will work, I'm not sure, I haven't used it in some time
 
.
Sandmann999,

Here's a simple routine that will display the results necessary to see what certain key combos will produce. I think you will be disappointed, though.

One of the few things you can do is press "x" and you'll receive 120. If you hold down ALT and "x", you'll receive 0 + 45, so you can have some variations on a theme.

In your INKEY$ routine, it's easier to have the program respond to a simple "x" rather than CHR$(120). But if the combo shows two different numbers, use CHR$(0) + CHR$(45). Example:

If A$ = CHR$(0) + CHR$(45) THEN do this, etc.
If A$ = "x" THEN do this, etc.

Dr. Jon


''' The Routine '''
a:
a$ = INKEY$: IF a$ = "" THEN GOTO a
PRINT ASC(LEFT$(a$, 1)); ASC(RIGHT$(a$, 1))
GOTO a

 
See my recent post to thread "Game: keyboard input" thread314-243384
 
Thank you Dr. Jon for that peice of code, that'll certainly help me out! That'll be awesome for figuring out what I need to look for when trying to find keys such as the function keys. :) Thanks.
But my main question still remains. How do I recieve multiple inputs from the keyboard at one time? Such as if I want it if the player presses UP and LEFT at the same time, that it will produce a diagonal (instead of moving in the direction of which ever key was pressed last.)
I would be very greatful to anyone that could help me with this!
 
And again, more disappointment :) That code from the other post also detects multiple simultaneous keypresses.
 
Sorry to disappoint you.

That was a lot of code to look through, and without testing every peice of it, I wasn't able to tell what most of it did, and because of the topic of the discussion, it was to be believed that most of it dealt with speeding up the keyboard input or getting rid of the "pause" when pressing and holding a key.

I would be very appreciative if you could direct me to what part exactly dealt with the multiple simutaneous keyboard inputs.
Was it the code near the end that you directed my attention to in the other post?
 
A lot of code, eh? :)

The code works by maintaining an array of key states. The '30 times a second' number was pulled out of my hat, simply because it is very unlikely that anybody could press two different keys within 1/30th of a second (even when mashing the keys). In reality, it is "as much as possible". In the preceding paragraphs, I described how the keyboard controller updates the value it returns from the port and then raises the interrupt. What I did not mention (but which was implied) is that that value can be read as many times as you like, and at any time, up until the next keypress when it gets overwritten. It is based on this that I "poll" it, so to say, many times a second.

The main body of the code is very simple in this case: the key state array is declared, and then it enters a loop which repeatedly calls the update function and then shows on the screen the state of every key on the keyboard. The actual work -- that of maintaining the array -- is done by the small [tt]SUB updateKeyboardArray[/tt]. To use the routine in your code, you need this routine, and one other thing: you need to declare the keyboard state array, as follows:
[tt]
DIM keyDown%(127)
[/tt]
Then, inside your code, your main loop, make sure that [tt]updateKeyboardArray[/tt] is called as often as possible, and at least 30 times a second, and test if keys are down by reading the corresponding elements in the [tt]keyDown%()[/tt] array. A complete list of scan codes is in the help file. The easiest way to get to it is to look up the [tt]ON KEY[/tt] help page.

Good luck :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top