detecting keys held down

need more understanding

Page 1 of 1

3 Replies - 750 Views - Last Post: 21 June 2010 - 10:47 AM Rate Topic: -----

#1 calvinthedestroyer  Icon User is offline

  • D.I.C Lover

Reputation: 167
  • View blog
  • Posts: 1,913
  • Joined: 13-October 07

detecting keys held down

Posted 07 June 2010 - 02:54 PM

Here is some QBASIC code that I found on another forum that shows the status of the keyboard keys. (the other forum is closed so I can't ask them)

I added the line numbers for reference.
01 DECLARE SUB updateKeyboardArray ()
02 DIM keyDown%(127) '127 is the maximum scan code

03 CLS
04 DO
05  updateKeyboardArray 'must be called at least 30 times per second

06  LOCATE 1, 1
07  FOR i% = 0 TO 127
08  IF (i% AND 15) = 0 THEN PRINT
09     IF keyDown%(i%) THEN
10        COLOR 15
11        PRINT "1";
12     ELSE
13        COLOR 7
14        PRINT "0";
15     END IF
16  NEXT i%
17 LOOP UNTIL keyDown%(1) ' scan code 1 corresponds to the escape key

18 SUB updateKeyboardArray
19 SHARED keyDown%()

20  byteValue% = INP(&H60)

21  status% = (byteValue% AND 128) <> 0
22  scanCode% = (byteValue% AND 127)

23  keyBeingPressed% = (status% = 0)
24  keyDown%(scanCode%) = keyBeingPressed%
25 END SUB



This program works well but I don't understand what is going on on line 08?

Plus how would you do this in C?

Is This A Good Question/Topic? 0
  • +

Replies To: detecting keys held down

#2 calvinthedestroyer  Icon User is offline

  • D.I.C Lover

Reputation: 167
  • View blog
  • Posts: 1,913
  • Joined: 13-October 07

Re: detecting keys held down

Posted 10 June 2010 - 06:43 PM

Well I found out what line 08 does, I just don't under stand why.

Line 08 creates a carriage return. Basically if i% equals an interval of 15 then create a carriage return. The whole line just doesn't make sense, print does not create a carriage return but in this program it does. i% only equals 15 one time but in this program it acts more like a counter.

I'm working on rewriting this program (in a way that I can understand) I just wish there was someone that can explain why this program does what it does.
:)
Was This Post Helpful? 0
  • +
  • -

#3 dchw  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 07-February 09

Re: detecting keys held down

Posted 13 June 2010 - 10:20 PM

At first glance, line 08 looks to be a bitwise AND of the two integers. If so, since QBASIC uses ASCII code for key recognition, we need to see what 15 in decimal is in ASCII.

15 (decimal) = 0F (Hex) = "Shift In" in ASCII. It appears that line 08 simply detects if shift is being held down or not when recognizing keypresses.

I don't have QBASIC on my Linux box yet, maybe I will give it a shot later when I can.

In C++, you would need the ASCII value of the keypress(some byte or char holding it in binary), and then simply "and" it with the representation of left shift(char or byte.)

EDIT:
And I thought PRINT generated a carriage return(blank line) if it had no data? See for reference:
http://www.qbasicsta...c=t_beg&t=begin

This post has been edited by dchw: 13 June 2010 - 10:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 calvinthedestroyer  Icon User is offline

  • D.I.C Lover

Reputation: 167
  • View blog
  • Posts: 1,913
  • Joined: 13-October 07

Re: detecting keys held down

Posted 21 June 2010 - 10:47 AM

here is what I rewrote:
DECLARE SUB updateKeyboardArray ()
DIM keyDown%(127) '127 is the maximum scan code

SCREEN 12  ' 80 x 60 text format
WIDTH 80, 60   'LOCATE down, 'over set my font size
CLS
x = 4       'start 4 rows down
y = 1
i% = 0
LOCATE 2, 1: PRINT "| CHR  % | VAL  % | CHR  % | VAL  % | CHR  % | VAL  % | CHR  % | VAL  % |"
  DO WHILE y < 70
      DO WHILE x < 36                          'makes our colums 32 rows high
         LOCATE x, y: PRINT "| chr"; i%            'prints the key value
         LOCATE x, (y + 9): PRINT "| val"
         x = x + 1                                 'move down to next row
         i% = i% + 1                               'move to next key value
      LOOP
      x = 4            'set our offset, start printing 4 rows down
      y = y + 18       'move to our next colum
  LOOP
LOCATE 38, 5: INPUT "enter="; enter$      'for debug steping
'******************** end startup *****************************************

x = 4       'start 4 rows down
y = 16
i% = 0
DO
updateKeyboardArray 'must be called at least 30 times per second
  DO WHILE y < 80
      DO WHILE x < 36                          'makes our colums 32 rows high
           IF keyDown%(i%) THEN                    'Is that key pressed down?
              LOCATE x, y: PRINT "1"
           ELSE
              LOCATE x, y: PRINT "0"               'no it ain't pressed down
           END IF
         x = x + 1                                 'move down to next row
         i% = i% + 1                               'move to next key value
         IF i% > 127 THEN : i% = 0
      LOOP
      x = 4            'set our offset, start printing 4 rows down
      y = y + 18       'move to our next colum
  LOOP
LOOP UNTIL keyDown%(1) ' scan code 1 corresponds to the escape key

SUB updateKeyboardArray
SHARED keyDown%()
 byteValue% = INP(&H60)

 status% = (byteValue% AND 128) <> 0
 scanCode% = (byteValue% AND 127)

 keyBeingPressed% = (status% = 0)
 keyDown%(scanCode%) = keyBeingPressed%
END SUB



The program prints out a table with the key numbers.
Then it loops though all the keys to see they are press down and just prints the "0" and "1"s

But It is not working. I am not showing that keys are pressed down. I think the program is not updating fast enough :(

I only care about the up, down, left and right arrow keys. is there a way to check to see if just those keys are pressed down?

For example. I need to check if the user is pressing down and kick for a low kick.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1