Author Topic: Keyhit/Keydown???  (Read 7402 times)

Offline baicskillet

  • Mc. Print
  • *
  • Posts: 29
    • View Profile
Re: Keyhit/Keydown???
« Reply #15 on: 2010-Feb-22 »
So, what does this keyhit program do?  I'm trying to learn glbasic, I'd appreciate your kindness.  :happy:

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: Keyhit/Keydown???
« Reply #16 on: 2010-Feb-22 »
You pass it a key that you wish to know the status of. It will return one of the following values.

//  0 = not pressed
//  2 = just pressed
//  1 = pressed
// -1 = release event

The standard keyboard commands in the language don't give you this detail (they can only tell you at a given time if a key is pressed or not).

Offline baicskillet

  • Mc. Print
  • *
  • Posts: 29
    • View Profile
Re: Keyhit/Keydown???
« Reply #17 on: 2010-Feb-22 »
You pass it a key that you wish to know the status of. It will return one of the following values.

//  0 = not pressed
//  2 = just pressed
//  1 = pressed
// -1 = release event

The standard keyboard commands in the language don't give you this detail (they can only tell you at a given time if a key is pressed or not).

Ah ok, thank you for that explanation.   =D  But, hmm, let's say I would like to use the standard keyboard commands to know if a key, any key, has been pressed or not.  What would I have to do?

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: Keyhit/Keydown???
« Reply #18 on: 2010-Feb-22 »
You have two ways of doing it
1) You stop your program until a key is pressed. Use the KEYWAIT/INPUT commands for this
2) If you don't want to stop your program then if you want to see if ANY key is pressed you'll need to use this keyhit function or a similar cut down one. Here's the cutdown version :- ie.
Code: (glbasic) [Select]
for KeyTest = 0 to 255
   if key(KeyTest) = 1
        // Do something
    endif
next
What this is doing is looping through all 256 possible keycodes and testing each to see if that key is currently pressed. If it is, then the result of "key(keycode being tested)" will equal 1.

Offline baicskillet

  • Mc. Print
  • *
  • Posts: 29
    • View Profile
Re: Keyhit/Keydown???
« Reply #19 on: 2010-Feb-22 »
Ah, awesome. I think i prefer to not stop the program.  I mostly need to check if any key has been pressed. As in a loop that executes commands as long as ANY key has been pressed.  I assume i'd have to use that for loop to check the whole 256 keys inside a function, and then make that function return either a 1 or a 0, right? Then, assign that returned number to a variable. 

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: Keyhit/Keydown???
« Reply #20 on: 2010-Feb-22 »
You don't need to put that loop inside a function (though that would be good programming style) - you could put those lines in the middle of your main loop if you wanted.

If you're going to assign the returned number to a varaible (i.e. you want to know which actual key was pressed rather than just if ANY key was pressed) then you might as well use the code at the start of this thread.

If you want to use the short version, my suggestion would be
Code: (glbasic) [Select]
FUNCTION KeyPressedTest:
    FOR KeyTest = 0 TO 255
        IF KEY(KeyTest) = 1
            return KeyTest
        ENDIF
    NEXT
    RETURN -1
ENDFUNCTION
That way you'll have a return code of -1 if no key was pressed, or the keycode of the pressed key if one is pressed. This is easier than returning either 1 or 0 and having a separate variable as well.

The problem with using this small function is that it stops when it detects the first key pressed. If you have 2 keys pressed at the same time, it will only ever return one of them.

If you only wanted to test for a few keys (e.g. ESC, Space or Enter) then using individual "IF KEY(...)" statements might be easier.
« Last Edit: 2010-Feb-22 by FutureCow »