Beginner ? on KeyHit and KeyDown

BlitzMax Forums/BlitzMax Beginners Area/Beginner ? on KeyHit and KeyDown

DragonProgrammer(Posted 2006) [#1]
I am a beginner. Just got the program. This simple program won't recognize the keystrokes (Enter or ESC). Anyone know why not? Thanks.


; KeyDown() example

Print "Hold down ENTER key!"
Delay 3000
While Not KeyHit(1)
If KeyDown(28) Then
Print "Enter is being pressed!"
Else
Print "No key is being pressed"
End If
Wend


FlameDuck(Posted 2006) [#2]
You need to be in 'Graphics mode' for polled input to work. Try putting a 'Graphics 64,64' at the top of your code.
Graphics 64,64
Print "Hold down ENTER key!"
While Not KeyHit(KEY_ESCAPE)
If KeyDown(KEY_ENTER) Then
Print "Enter is being pressed!"
Else
Print "No key is being pressed"
End If
Delay 500
Wend 



DragonProgrammer(Posted 2006) [#3]
I'm not sure what you mean by 'Graphics ModeIs'. Is there more code to the program above? It didn't work when I copy and pasted. =]


Grisu(Posted 2006) [#4]
For most cases you don't need KeyDown.
If you have a menu or options to tigger KeyHit is all you need.

NEVER USE DELAY IN A MAIN LOOP!
Your key detection will be buggy / laggy because of it
and your program will run at different speeds on other PCs.


Here's a tiny example code:

Graphics 800,600,0 ' init windowed gfxmode

While (Not AppTerminate()) And (KeyHit(KEY_ESCAPE)=False) ' Do as long as ESC is pressed or "close" button on window is clicked.

Cls

SetColor 255,255,255
DrawText " Press the SPACE or ENTER key",20,20 

If KeyDown(KEY_SPACE) Then SetColor 0,255,0 Else SetColor 255,0,0

DrawText " SPACE KEY ",20,40

If KeyDown(KEY_ENTER) Then SetColor 0,255,0 Else SetColor 255,0,0

DrawText " ENTER KEY ",20,60

Flip ' show stuff on front buffer

Wend ' of main loop

End ' of app