Author Topic: keyboard input (how do i read without repeat so it runs just like a joystick)  (Read 3463 times)

0 Members and 1 Guest are viewing this topic.

Offline PMACKAY

  • Forum Regular
  • Posts: 188
  • LIFE is Temporary
    • View Profile
hi below is my keyboard routine... it runs great but how can i run the same input without the keyboard repeat delay

i want to just press the keys and they will respond like a joystick... holding the key has a delay then repeat.
 
this is for my game i am making which so far feel really cool to play...

this is a remake of my digit (ASCII graphics boulder but written from complete scratch with multi color graphics and 8 frame animation.. just not ready yet to demo.. but is pretty cool


Code: QB64: [Select]
  1.    KH& = _KEYHIT: SHFT% = _KEYDOWN(100303) OR _KEYDOWN(100304): ESC% = _KEYDOWN(27)
  2.     IF KH& = -18432 THEN
  3.         Z% = SCREENMEM%(MJ% - 1, MI%)
  4.         IF Z% = DIRT THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% - 1, MI%) = MAN: MJ% = MJ% - 1
  5.         IF Z% = SPACE THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% - 1, MI%) = MAN: MJ% = MJ% - 1
  6.         IF Z% = DIAMOND THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% - 1, MI%) = MAN: MJ% = MJ% - 1
  7.         'UP
  8.     END IF
  9.     IF KH& = -20480 THEN
  10.         Z% = SCREENMEM%(MJ% + 1, MI%)
  11.         IF Z% = DIRT THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% + 1, MI%) = MAN: MJ% = MJ% + 1
  12.         IF Z% = SPACE THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% + 1, MI%) = MAN: MJ% = MJ% + 1
  13.         IF Z% = DIAMOND THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% + 1, MI%) = MAN: MJ% = MJ% + 1
  14.         'DOWN
  15.     END IF
  16.     IF KH& = -19200 AND SHFT% = -1 THEN
  17.         Z1% = SCREENMEM%(MJ%, MI% - 1)
  18.         Z2% = SCREENMEM%(MJ%, MI% + 1)
  19.         IF Z1% = DIRT AND Z2% = BOULDER THEN SCREENMEM%(MJ%, MI% - 1) = MAN: SCREENMEM%(MJ%, MI%) = BOULDER: SCREENMEM%(MJ%, MI% + 1) = SPACE: MI% = MI% - 1: GOTO NEXTM
  20.         IF Z1% = SPACE AND Z2% = BOULDER THEN SCREENMEM%(MJ%, MI% - 1) = MAN: SCREENMEM%(MJ%, MI%) = BOULDER: SCREENMEM%(MJ%, MI% + 1) = SPACE: MI% = MI% - 1: GOTO NEXTM
  21.         IF Z1% = SPACE AND Z2% = DIAMOND THEN SCREENMEM%(MJ%, MI% - 1) = MAN: SCREENMEM%(MJ%, MI%) = DIAMOND: SCREENMEM%(MJ%, MI% + 1) = SPACE: MI% = MI% - 1: GOTO NEXTM
  22.         GOTO NEXTM
  23.         'LEFT SHIFT
  24.     END IF
  25.     IF KH& = -19200 THEN
  26.         Z% = SCREENMEM%(MJ%, MI% - 1)
  27.         IF Z% = DIRT THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% - 1) = MAN: MI% = MI% - 1
  28.         IF Z% = SPACE THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% - 1) = MAN: MI% = MI% - 1: SCY1% = SCY1% - 1
  29.         IF Z% = DIAMOND THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% - 1) = MAN: MI% = MI% - 1
  30.         IF Z% = BOULDER AND SCREENMEM%(MJ%, MI% - 2) = SPACE THEN SCREENMEM%(MJ%, MI% - 2) = BOULDER: SCREENMEM%(MJ%, MI% - 1) = MAN: SCREENMEM%(MJ%, MI%) = SPACE: MI% = MI% - 1
  31.         'LEFT
  32.     END IF
  33.     IF KH& = -19712 AND SHFT% = -1 THEN
  34.         Z1% = SCREENMEM%(MJ%, MI% + 1)
  35.         Z2% = SCREENMEM%(MJ%, MI% - 1)
  36.         IF Z1% = DIRT AND Z2% = BOULDER THEN SCREENMEM%(MJ%, MI% + 1) = MAN: SCREENMEM%(MJ%, MI%) = BOULDER: SCREENMEM%(MJ%, MI% - 1) = SPACE: MI% = MI% + 1: GOTO NEXTM
  37.         IF Z1% = SPACE AND Z2% = BOULDER THEN SCREENMEM%(MJ%, MI% + 1) = MAN: SCREENMEM%(MJ%, MI%) = BOULDER: SCREENMEM%(MJ%, MI% - 1) = SPACE: MI% = MI% + 1: GOTO NEXTM
  38.         IF Z1% = SPACE AND Z2% = DIAMOND THEN SCREENMEM%(MJ%, MI% + 1) = MAN: SCREENMEM%(MJ%, MI%) = DIAMOND: SCREENMEM%(MJ%, MI% - 1) = SPACE: MI% = MI% + 1: GOTO NEXTM
  39.         GOTO NEXTM
  40.         'RIGHT SHIFT
  41.     END IF
  42.  
  43.     IF KH& = -19712 THEN
  44.         Z% = SCREENMEM%(MJ%, MI% + 1)
  45.         IF Z% = DIRT THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% + 1) = MAN: MI% = MI% + 1
  46.         IF Z% = SPACE THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% + 1) = MAN: MI% = MI% + 1
  47.         IF Z% = DIAMOND THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% + 1) = MAN: MI% = MI% + 1
  48.         IF Z% = BOULDER AND SCREENMEM%(MJ%, MI% + 2) = SPACE THEN SCREENMEM%(MJ%, MI% + 2) = BOULDER: SCREENMEM%(MJ%, MI% + 1) = MAN: SCREENMEM%(MJ%, MI%) = SPACE: MI% = MI% + 1
  49.         'RIGHT
  50.     END IF
  51.     NEXTM:
  52. LOOP UNTIL ESC% = -1
« Last Edit: July 05, 2019, 10:37:47 am by odin »
MackyWhite

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Hi PMACKAY,

_KEYCLEAR has helped me at times with repeating keys.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Hi PMACKAY,

_KEYCLEAR has helped me at times with repeating keys.

Or setting a flag for the direction might work for you, similar to the pseudo code below.

K = _KEYHIT

SELECT CASE K
   CASE -12345 ‘no idea the actual value for down, so I invented one for example
       Down = -1
       Up = 0 ‘You can’t move up and down both at once.
   CASE +12345 ‘the (fake value) for the down key is now up
       Down = 0
   CASE other keys....
END SELECT

IF Down THEN
    ‘Do the down movement stuff
END IF
IF LeftFlag/RightFlag/UpFlag THEN
    ‘Do that stuff
END IF
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi
you can use a medium variable to manage input...

1.take the input: es. Keyboard =_keyhit....

2.store input coded in a variable: es. IF keyboard = 3  then Move = "Fly"

3. evaluate if input is changed  es. IF OldMove = Move then...

4.  take a memory of coded input: es.  OldMove = Move

5. execute command  IF Move = "Fly" then....

6. if you plan to make one move for one Keyboard's input  here you must reset the variable of movement: es. Move = ""
as alternate movement feature you can leave  the Move value until you get a new input from keyboard

here a my mod of your code
(I have left the label and the GOTO in the inner engine because I don't know if those options are exaustive  or not, I prefer not to use GOTO and GOSUB if possible:-) )
Code: QB64: [Select]
  1.     KH& = _KEYHIT: SHFT% = _KEYDOWN(100303) OR _KEYDOWN(100304): ESC% = _KEYDOWN(27)
  2.     Move$ = "" ' an extra layer between  I/O function and Engine making moves
  3.  
  4.     IF KH& = -18432 THEN
  5.         Move$ = "UP"
  6.     ELSEIF KH& = -20480 THEN
  7.         Move$ = "DOWN"
  8.     ELSEIF KH& = -19200 AND SHFT% = -1 THEN
  9.         Move$ = "LEFTSHIFT"
  10.         '        GOTO NEXTM  <---- you don't need this and the correlate label
  11.     ELSEIF KH& = -19200 THEN
  12.         Move$ = "LEFT"
  13.     ELSEIF KH& = -19712 AND SHFT% = -1 THEN
  14.         Move$ = "RIGHTSHIFT"
  15.         '       GOTO NEXTM   <---- you don't need this and the correlate label
  16.     ELSEIF KH& = -19712 THEN
  17.         Move$ = "RIGHT"
  18.     END IF
  19.  
  20.  
  21.     'engine of movement
  22.     IF Move$ = "UP" THEN
  23.         Z% = SCREENMEM%(MJ% - 1, MI%)
  24.         IF Z% = DIRT THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% - 1, MI%) = MAN: MJ% = MJ% - 1
  25.         IF Z% = SPACE THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% - 1, MI%) = MAN: MJ% = MJ% - 1
  26.         IF Z% = DIAMOND THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% - 1, MI%) = MAN: MJ% = MJ% - 1
  27.         'UP
  28.     ELSEIF Move$ = "DOWN" THEN
  29.         Z% = SCREENMEM%(MJ% + 1, MI%)
  30.         IF Z% = DIRT THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% + 1, MI%) = MAN: MJ% = MJ% + 1
  31.         IF Z% = SPACE THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% + 1, MI%) = MAN: MJ% = MJ% + 1
  32.         IF Z% = DIAMOND THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ% + 1, MI%) = MAN: MJ% = MJ% + 1
  33.         'DOWN
  34.     ELSEIF Move$ = "LEFTSHIFT" THEN
  35.         Z1% = SCREENMEM%(MJ%, MI% - 1)
  36.         Z2% = SCREENMEM%(MJ%, MI% + 1)
  37.         IF Z1% = DIRT AND Z2% = BOULDER THEN SCREENMEM%(MJ%, MI% - 1) = MAN: SCREENMEM%(MJ%, MI%) = BOULDER: SCREENMEM%(MJ%, MI% + 1) = SPACE: MI% = MI% - 1: GOTO NEXTM
  38.         IF Z1% = SPACE AND Z2% = BOULDER THEN SCREENMEM%(MJ%, MI% - 1) = MAN: SCREENMEM%(MJ%, MI%) = BOULDER: SCREENMEM%(MJ%, MI% + 1) = SPACE: MI% = MI% - 1: GOTO NEXTM
  39.         IF Z1% = SPACE AND Z2% = DIAMOND THEN SCREENMEM%(MJ%, MI% - 1) = MAN: SCREENMEM%(MJ%, MI%) = DIAMOND: SCREENMEM%(MJ%, MI% + 1) = SPACE: MI% = MI% - 1: GOTO NEXTM
  40.         'LEFT SHIFT
  41.     ELSEIF Move$ = "LEFT" THEN
  42.         Z% = SCREENMEM%(MJ%, MI% - 1)
  43.         IF Z% = DIRT THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% - 1) = MAN: MI% = MI% - 1
  44.         IF Z% = SPACE THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% - 1) = MAN: MI% = MI% - 1: SCY1% = SCY1% - 1
  45.         IF Z% = DIAMOND THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% - 1) = MAN: MI% = MI% - 1
  46.         IF Z% = BOULDER AND SCREENMEM%(MJ%, MI% - 2) = SPACE THEN SCREENMEM%(MJ%, MI% - 2) = BOULDER: SCREENMEM%(MJ%, MI% - 1) = MAN: SCREENMEM%(MJ%, MI%) = SPACE: MI% = MI% - 1
  47.         'LEFT
  48.     ELSEIF Move$ = "RIGHTSHIFT" THEN
  49.         Z1% = SCREENMEM%(MJ%, MI% + 1)
  50.         Z2% = SCREENMEM%(MJ%, MI% - 1)
  51.         IF Z1% = DIRT AND Z2% = BOULDER THEN SCREENMEM%(MJ%, MI% + 1) = MAN: SCREENMEM%(MJ%, MI%) = BOULDER: SCREENMEM%(MJ%, MI% - 1) = SPACE: MI% = MI% + 1: GOTO NEXTM
  52.         IF Z1% = SPACE AND Z2% = BOULDER THEN SCREENMEM%(MJ%, MI% + 1) = MAN: SCREENMEM%(MJ%, MI%) = BOULDER: SCREENMEM%(MJ%, MI% - 1) = SPACE: MI% = MI% + 1: GOTO NEXTM
  53.         IF Z1% = SPACE AND Z2% = DIAMOND THEN SCREENMEM%(MJ%, MI% + 1) = MAN: SCREENMEM%(MJ%, MI%) = DIAMOND: SCREENMEM%(MJ%, MI% - 1) = SPACE: MI% = MI% + 1: GOTO NEXTM
  54.         'RIGHT SHIFT
  55.     ELSEIF Move$ = "RIGHT" THEN
  56.         Z% = SCREENMEM%(MJ%, MI% + 1)
  57.         IF Z% = DIRT THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% + 1) = MAN: MI% = MI% + 1
  58.         IF Z% = SPACE THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% + 1) = MAN: MI% = MI% + 1
  59.         IF Z% = DIAMOND THEN SCREENMEM%(MJ%, MI%) = SPACE: SCREENMEM%(MJ%, MI% + 1) = MAN: MI% = MI% + 1
  60.         IF Z% = BOULDER AND SCREENMEM%(MJ%, MI% + 2) = SPACE THEN SCREENMEM%(MJ%, MI% + 2) = BOULDER: SCREENMEM%(MJ%, MI% + 1) = MAN: SCREENMEM%(MJ%, MI%) = SPACE: MI% = MI% + 1
  61.         'RIGHT
  62.     END IF
  63.     NEXTM: ' This label is used also in RightShift and LeftShift commands...
  64.     'but if the three conditions are the only to manage
  65.     'you can nest  also this 3 IF THEN in the chain of IF THEN
  66.     'and avoid the label
  67. LOOP UNTIL ESC% = -1
  68.  

Thanks to read
Programming isn't difficult, only it's  consuming time and coffee

Offline PMACKAY

  • Forum Regular
  • Posts: 188
  • LIFE is Temporary
    • View Profile
thank you guys :) much appreciated.... still working on other routines also. but going great. stuck a snapshot on the work sofar.

« Last Edit: July 08, 2019, 06:49:22 am by PMACKAY »
MackyWhite

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Hi PMACKAY,

Nice artwork, I assume it's yours. Any luck with _KEYCLEAR, setting flags or intermediate variables to actions?