Author Topic: Can I check to see what KEY() is being pressed?  (Read 4124 times)

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
I'm curious to know if there's an easy way to implement...

Code: (glbasic) [Select]
local a%
a%=KEY()
PRINT a%, 0, 0, TRUE

Presently KEY() expects a scancode for a true/false value to be assigned to a%, I was hoping KEY() would give true/false for ANY key, or better still an actual scancode otherwise 0

Any thoughts on this...?
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1621
  • Particle Hawk
    • View Profile
This is a realtime key detection function. It must be used in a realtime loop.
use it like this:
Keyspressed = Sys_KeyBoardIO()

Once a key is released, the variable will hold which one it was...for one frame of time.
It will report all keys pressed, it is storing them in an array and reports them one at a time.
So if you press 4 keys...and release them....it will need 4 frames to report all 4.
Persoanlly, i use this and it works good...you just need to setup a 'select case' thingy.

Code: (glbasic) [Select]
// ------------------------------------------------------------- //
// ---  KeyBoardIO  --- detect any key press...great for user inputs
// waits till button is released...then returns that keycode.
// Doesnt do key pressed pause and repeat ...just a release return
// This will fire all key status into RETURN, one at a time from buffer..
// This allows you to do a check on all buttons pressed, not just one at a time.
// ------------------------------------------------------------- //
FUNCTION Sys_KeyBoardIO:

LOCAL KeyIndex
LOCAL IsKeyPressed = 0
LOCAL IsKeyLocked = 1
STATIC INITIALIZE

IF INITIALIZE = FALSE; INITIALIZE = TRUE;
DIM KeyBuffer[ 221 ][ 2 ]
KeyBuffer[ 0 ][ 0 ] = 0
ENDIF

//Check all key holding states:
FOR KeyIndex = 0 TO 220
IF KEY( KeyIndex )
KeyBuffer[ KeyIndex ][ IsKeyPressed ] = TRUE //Key pressed for the first time.
IF KeyBuffer[ KeyIndex ][ IsKeyLocked ] = FALSE
KeyBuffer[ KeyIndex ][ IsKeyLocked ] = TRUE
RETURN KeyIndex
ENDIF
ELSE
KeyBuffer[ KeyIndex][ IsKeyPressed ] =FALSE //Key held
KeyBuffer[ KeyIndex][ IsKeyLocked ] = FALSE
ENDIF
NEXT

RETURN -1 //Send NULL value

ENDFUNCTION
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3955
    • View Profile
    • Space Fractal
I do a similar thing. Also in Greedy Mouse I use J_DOWN, J_UP, J_RIGHT, J_LEFT, J_BUTTON1, J_BUTTON2 variables which later could been remapped to any key. Dont do hardwired keys directly with values.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation and The beagle Jam.

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4159
  • On the shoulders of giants.
    • View Profile
    • My Apps
Code: (glbasic) [Select]
LOCAL a%
WHILE TRUE

FOR n=0 to 250
 IF KEY(n) THEN a=n
NEXT

PRINT a,10,10

 SHOWSCREEN
WEND

I use something like that in my redefine keys functions and store the value in an array. I also store the actual key names in an array so that if I select say key 28 the array button$[28]="ENTER" is accessed and "ENTER" is displayed when the user presses the button.

Not really sure if that's what you're after though.
I came. I saw. I played.

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
Any of these are ideal, I will opt for Ian's method as it fits what I need, I was just hoping there was a parameter for the KEY() command so I didn't have to iterate through a loop to check all possibilities. It does make me wonder what the keycode would be for say SHIFT + ENTER this is why I was hoping to attempt to just check KEY().

Thank you all for your replies :)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1621
  • Particle Hawk
    • View Profile
Any of these are ideal, I will opt for Ian's method as it fits what I need, I was just hoping there was a parameter for the KEY() command so I didn't have to iterate through a loop to check all possibilities. It does make me wonder what the keycode would be for say SHIFT + ENTER this is why I was hoping to attempt to just check KEY().

Thank you all for your replies :)


Ians method is good for reporting a held and holding key one key at a time. If 2 are held, it will only report the highest numerical key value.

You can combine key patterns using my function. Use a timer to see how far apart in time the 2 keys were released (ie 200ms or something).

Ultimately you need to figgure out whether you want held or released key detection.
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2747
    • View Profile
For simple combination keys I use this:

Code: (glbasic) [Select]
    //200=up, 208=down, 203=left, 205=right, 42=left SHIFT, 54=right SHIFT,29=left CTRL, 157=right CTRL (bit 0-7)
    INLINE
heldKeys=KEY(200)|(KEY(208)<<1)|(KEY(203)<<2)|(KEY(205)<<3)|(KEY(42)<<4)|(KEY(54)<<5)|(KEY(29)<<6)|(KEY(157)<<7);
ENDINLINE

This line creates a byte with certain bits representing certain keys. This makes it quite efficient at checking combinations of certain keys, you can do so in one bAND() command.

You put this inline into wherever you check scan your input for each loop.

To check if a key is being held down:

Code: (glbasic) [Select]
IF bAND(heldKeys%,1) THEN DEC z,10 // Up cursor
IF bAND(heldKeys%,64) AND KEY(30) THEN DEC z,10 // Left CTRL+"A"
IF bAND(heldKeys%,64+128) AND KEY(30) THEN INC z,10 // Any CTRL + "A"
IF bAND(heldKeys%,1+16+32) THEN INC z,10 // Any SHIFT + Left Cursor

So you need a global called "heldKeys%" and you use a logical binary AND of the bits your interested in, eg "1+128"=bit 0(left Cursor)+bit 7(right CTRL).

This would probably look better with some constants for the binary stuff here (eg. RIGHTCTRL=128) but you can add that if you like.

« Last Edit: 2012-Nov-06 by bigsofty »
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1621
  • Particle Hawk
    • View Profile
didnt know you can do that with key >.<

im moving this topic into codesnippets.
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)