I've found the thread by Gernot but for some reason I cann't see what is posted. It's just a blank post. really need to know if there is a keyhit command. I need it for my game.
Hope someone can help! :)
Sure thing.
I don't have the original version, this is my altered version but works similarly. Just make sure to copy that "GLOBAL gKeys AS KEYCODES" so you don't get strange errors when trying to use the key names :-)
// --------------------------------- //
// Project: KeyHit
// Start: Saturday, September 22, 2007
// IDE Version: 5.022
// Example:
//GLOBAL gKeys AS KEYCODES
//
//WHILE TRUE
// KeyHitUpdate()
//
// PRINT "SpaceBar-State:" + KeyHit(57), 100,100
// IF KeyHit(57)=2
// INC tips, 1
// PRINT "*BING*", 100, 120
// ENDIF
// IF KeyHit(57)=-1
// PRINT "*BONG*", 100, 120
// ENDIF
//
// IF KEY(gKeys.space) THEN PRINT "Space", 100, 100
//
// PRINT "Tipps"+tips, 100, 140
//
// HIBERNATE
// SHOWSCREEN
//WEND
//
TYPE KEYCODES
vk_escape = 0x01
vk_1 = 0x02
vk_2 = 0x03
vk_3 = 0x04
vk_4 = 0x05
vk_5 = 0x06
vk_6 = 0x07
vk_7 = 0x08
vk_8 = 0x09
vk_9 = 0x0A
vk_0 = 0x0B
vk_minus = 0x0C // - on main keyboard
vk_equals = 0x0D
vk_back = 0x0E // backspace
vk_tab = 0x0F
vk_q = 0x10
vk_w = 0x11
vk_e = 0x12
vk_r = 0x13
vk_t = 0x14
vk_y = 0x15
vk_u = 0x16
vk_i = 0x17
vk_o = 0x18
vk_p = 0x19
vk_lbracket = 0x1A
vk_rbracket = 0x1B
vk_return = 0x1C // Enter on main keyboard
vk_lcontrol = 0x1D
vk_a = 0x1E
vk_s = 0x1F
vk_d = 0x20
vk_f = 0x21
vk_g = 0x22
vk_h = 0x23
vk_j = 0x24
vk_k = 0x25
vk_l = 0x26
vk_semicolon = 0x27
vk_apostrophe = 0x28
vk_grave = 0x29 // accent grave
vk_lshift = 0x2A
vk_backslash = 0x2B
vk_z = 0x2C
vk_x = 0x2D
vk_c = 0x2E
vk_v = 0x2F
vk_b = 0x30
vk_n = 0x31
vk_m = 0x32
vk_comma = 0x33
vk_period = 0x34 // . on main keyboard
vk_slash = 0x35 // / on main keyboard
vk_rshift = 0x36
vk_multiply = 0x37 // * on numeric keypad
vk_lmenu = 0x38 // left Alt
vk_space = 0x39
vk_capital = 0x3A
vk_f1 = 0x3B
vk_f2 = 0x3C
vk_f3 = 0x3D
vk_f4 = 0x3E
vk_f5 = 0x3F
vk_f6 = 0x40
vk_f7 = 0x41
vk_f8 = 0x42
vk_f9 = 0x43
vk_f10 = 0x44
vk_numlock = 0x45
vk_scroll = 0x46 // Scroll Lock
vk_numpad7 = 0x47
vk_numpad8 = 0x48
vk_numpad9 = 0x49
vk_subtract = 0x4A // - on numeric keypad
vk_numpad4 = 0x4B
vk_numpad5 = 0x4C
vk_numpad6 = 0x4D
vk_add = 0x4E // + on numeric keypad
vk_numpad1 = 0x4F
vk_numpad2 = 0x50
vk_numpad3 = 0x51
vk_numpad0 = 0x52
vk_decimal = 0x53 // . on numeric keypad
vk_oem_102 = 0x56 // < > | on UK/Germany keyboards
vk_f11 = 0x57
vk_f12 = 0x58
vk_f13 = 0x64 // (NEC PC98)
vk_f14 = 0x65 // (NEC PC98)
vk_f15 = 0x66 // (NEC PC98)
vk_kana = 0x70 // (Japanese keyboard)
vk_abnt_c1 = 0x73 // / ? on Portugese (Brazilian) keyboards
vk_convert = 0x79 // (Japanese keyboard)
vk_noconvert = 0x7B // (Japanese keyboard)
vk_yen = 0x7D // (Japanese keyboard)
vk_abnt_c2 = 0x7E // Numpad . on Portugese (Brazilian) keyboards
vk_numpadequals = 0x8D // = on numeric keypad (NEC PC98)
vk_prevtrack = 0x90 // Previous Track (vk_CIRCUMFLEX on Japanese keyboard)
vk_at = 0x91 // (NEC PC98)
vk_colon = 0x92 // (NEC PC98)
vk_underline = 0x93 // (NEC PC98)
vk_kanji = 0x94 // (Japanese keyboard)
vk_stop = 0x95 // (NEC PC98)
vk_ax = 0x96 // (Japan AX)
vk_unlabeled = 0x97 // (J3100)
vk_nexttrack = 0x99 // Next Track
vk_numpadenter = 0x9C // Enter on numeric keypad
vk_rcontrol = 0x9D
vk_mute = 0xA0 // Mute
vk_calculator = 0xA1 // Calculator
vk_playpause = 0xA2 // Play / Pause
vk_mediastop = 0xA4 // Media Stop
vk_volumedown = 0xAE // Volume -
vk_volumeup = 0xB0 // Volume +
vk_webhome = 0xB2 // Web home
vk_numpadcomma = 0xB3 // , on numeric keypad (NEC PC98)
vk_divide = 0xB5 // / on numeric keypad
vk_sysrq = 0xB7
vk_rmenu = 0xB8 // right Alt
vk_pause = 0xC5 // Pause
vk_home = 0xC7 // Home on arrow keypad
vk_up = 0xC8 // UpArrow on arrow keypad
vk_prior = 0xC9 // PgUp on arrow keypad
vk_left = 0xCB // LeftArrow on arrow keypad
vk_right = 0xCD // RightArrow on arrow keypad
vk_end = 0xCF // End on arrow keypad
vk_down = 0xD0 // DownArrow on arrow keypad
vk_next = 0xD1 // PgDn on arrow keypad
vk_insert = 0xD2 // Insert on arrow keypad
vk_delete = 0xD3 // Delete on arrow keypad
vk_lwin = 0xDB // Left Windows key
vk_rwin = 0xDC // Right Windows key
vk_apps = 0xDD // AppMenu key
vk_power = 0xDE // System Power
vk_sleep = 0xDF // System Sleep
vk_wake = 0xE3 // System Wake
vk_websearch = 0xE5 // Web Search
vk_webfavorites = 0xE6 // Web Favorites
vk_webrefresh = 0xE7 // Web Refresh
vk_webstop = 0xE8 // Web Stop
vk_webforward = 0xE9 // Web Forward
vk_webback = 0xEA // Web Back
vk_mycomputer = 0xEB // My Computer
vk_mail = 0xEC // Mail
vk_mediaselect = 0xED // Media Select
ENDTYPE
GLOBAL gKeys AS KEYCODES // Copy/move to your main file!
//
// Update keyboard state
FUNCTION KeyHitUpdate:
GLOBAL gKeyDown[], gKeyState[]
LOCAL k, i
// First Time call
IF BOUNDS(gKeyDown[],0)=0
DIM gKeyDown[256]
DIM gKeyState[256]
ENDIF
// For each key
FOR i=0 TO 255
k = KEY(i)
// Key is pressed
IF k
IF gKeyDown[i]
gKeyState[i]=1
ELSE
gKeyDown[i]=1
gKeyState[i]=2
ENDIF
ELSE
// Key is not pressed
// Has key been pressed before?
IF gKeyDown[i]
gKeyDown[i] = 0
gKeyState[i] = -1
ELSE
gKeyState[i]=0
ENDIF
ENDIF
NEXT
ENDFUNCTION
// 0 = not pressed
// 2 = just pressed
// 1 = pressed
// -1 = release event
FUNCTION KeyHit: nkey
RETURN gKeyState[nkey]
ENDFUNCTION
Hi Moru!
Thanks for the code, it works perfectly. Will this ever make it as standard in the compiler?
With the latest version of GLBasic (just updated) I get the following error in this code:
"keyhitc.gbas"(201) error : wrong argument type : KEY, arg no: 1
I don't have any troubles after updating, can you show a bit more of the code?
Here's the file:
// SETCURRENTDIR("Media") // seperate media and binaries?
/////////////KEYCODE INCLUDE///////////////////////////
// --------------------------------- //
// Project: KeyHit
// Start: Saturday, September 22, 2007
// IDE Version: 5.022
// Example:
//GLOBAL gKeys AS KEYCODES
//
//WHILE TRUE
// KeyHitUpdate()
//
// PRINT "SpaceBar-State:" + KeyHit(57), 100,100
// IF KeyHit(57)=2
// INC tips, 1
// PRINT "*BING*", 100, 120
// ENDIF
// IF KeyHit(57)=-1
// PRINT "*BONG*", 100, 120
// ENDIF
//
// IF KEY(gKeys.space) THEN PRINT "Space", 100, 100
//
// PRINT "Tipps"+tips, 100, 140
//
// HIBERNATE
// SHOWSCREEN
//WEND
//
TYPE KEYCODES
vk_escape = 0x01
vk_1 = 0x02
vk_2 = 0x03
vk_3 = 0x04
vk_4 = 0x05
vk_5 = 0x06
vk_6 = 0x07
vk_7 = 0x08
vk_8 = 0x09
vk_9 = 0x0A
vk_0 = 0x0B
vk_minus = 0x0C // - on main keyboard
vk_equals = 0x0D
vk_back = 0x0E // backspace
vk_tab = 0x0F
vk_q = 0x10
vk_w = 0x11
vk_e = 0x12
vk_r = 0x13
vk_t = 0x14
vk_y = 0x15
vk_u = 0x16
vk_i = 0x17
vk_o = 0x18
vk_p = 0x19
vk_lbracket = 0x1A
vk_rbracket = 0x1B
vk_return = 0x1C // Enter on main keyboard
vk_lcontrol = 0x1D
vk_a = 0x1E
vk_s = 0x1F
vk_d = 0x20
vk_f = 0x21
vk_g = 0x22
vk_h = 0x23
vk_j = 0x24
vk_k = 0x25
vk_l = 0x26
vk_semicolon = 0x27
vk_apostrophe = 0x28
vk_grave = 0x29 // accent grave
vk_lshift = 0x2A
vk_backslash = 0x2B
vk_z = 0x2C
vk_x = 0x2D
vk_c = 0x2E
vk_v = 0x2F
vk_b = 0x30
vk_n = 0x31
vk_m = 0x32
vk_comma = 0x33
vk_period = 0x34 // . on main keyboard
vk_slash = 0x35 // / on main keyboard
vk_rshift = 0x36
vk_multiply = 0x37 // * on numeric keypad
vk_lmenu = 0x38 // left Alt
vk_space = 0x39
vk_capital = 0x3A
vk_f1 = 0x3B
vk_f2 = 0x3C
vk_f3 = 0x3D
vk_f4 = 0x3E
vk_f5 = 0x3F
vk_f6 = 0x40
vk_f7 = 0x41
vk_f8 = 0x42
vk_f9 = 0x43
vk_f10 = 0x44
vk_numlock = 0x45
vk_scroll = 0x46 // Scroll Lock
vk_numpad7 = 0x47
vk_numpad8 = 0x48
vk_numpad9 = 0x49
vk_subtract = 0x4A // - on numeric keypad
vk_numpad4 = 0x4B
vk_numpad5 = 0x4C
vk_numpad6 = 0x4D
vk_add = 0x4E // + on numeric keypad
vk_numpad1 = 0x4F
vk_numpad2 = 0x50
vk_numpad3 = 0x51
vk_numpad0 = 0x52
vk_decimal = 0x53 // . on numeric keypad
vk_oem_102 = 0x56 // < > | on UK/Germany keyboards
vk_f11 = 0x57
vk_f12 = 0x58
vk_f13 = 0x64 // (NEC PC98)
vk_f14 = 0x65 // (NEC PC98)
vk_f15 = 0x66 // (NEC PC98)
vk_kana = 0x70 // (Japanese keyboard)
vk_abnt_c1 = 0x73 // / ? on Portugese (Brazilian) keyboards
vk_convert = 0x79 // (Japanese keyboard)
vk_noconvert = 0x7B // (Japanese keyboard)
vk_yen = 0x7D // (Japanese keyboard)
vk_abnt_c2 = 0x7E // Numpad . on Portugese (Brazilian) keyboards
vk_numpadequals = 0x8D // = on numeric keypad (NEC PC98)
vk_prevtrack = 0x90 // Previous Track (vk_CIRCUMFLEX on Japanese keyboard)
vk_at = 0x91 // (NEC PC98)
vk_colon = 0x92 // (NEC PC98)
vk_underline = 0x93 // (NEC PC98)
vk_kanji = 0x94 // (Japanese keyboard)
vk_stop = 0x95 // (NEC PC98)
vk_ax = 0x96 // (Japan AX)
vk_unlabeled = 0x97 // (J3100)
vk_nexttrack = 0x99 // Next Track
vk_numpadenter = 0x9C // Enter on numeric keypad
vk_rcontrol = 0x9D
vk_mute = 0xA0 // Mute
vk_calculator = 0xA1 // Calculator
vk_playpause = 0xA2 // Play / Pause
vk_mediastop = 0xA4 // Media Stop
vk_volumedown = 0xAE // Volume -
vk_volumeup = 0xB0 // Volume +
vk_webhome = 0xB2 // Web home
vk_numpadcomma = 0xB3 // , on numeric keypad (NEC PC98)
vk_divide = 0xB5 // / on numeric keypad
vk_sysrq = 0xB7
vk_rmenu = 0xB8 // right Alt
vk_pause = 0xC5 // Pause
vk_home = 0xC7 // Home on arrow keypad
vk_up = 0xC8 // UpArrow on arrow keypad
vk_prior = 0xC9 // PgUp on arrow keypad
vk_left = 0xCB // LeftArrow on arrow keypad
vk_right = 0xCD // RightArrow on arrow keypad
vk_end = 0xCF // End on arrow keypad
vk_down = 0xD0 // DownArrow on arrow keypad
vk_next = 0xD1 // PgDn on arrow keypad
vk_insert = 0xD2 // Insert on arrow keypad
vk_delete = 0xD3 // Delete on arrow keypad
vk_lwin = 0xDB // Left Windows key
vk_rwin = 0xDC // Right Windows key
vk_apps = 0xDD // AppMenu key
vk_power = 0xDE // System Power
vk_sleep = 0xDF // System Sleep
vk_wake = 0xE3 // System Wake
vk_websearch = 0xE5 // Web Search
vk_webfavorites = 0xE6 // Web Favorites
vk_webrefresh = 0xE7 // Web Refresh
vk_webstop = 0xE8 // Web Stop
vk_webforward = 0xE9 // Web Forward
vk_webback = 0xEA // Web Back
vk_mycomputer = 0xEB // My Computer
vk_mail = 0xEC // Mail
vk_mediaselect = 0xED // Media Select
ENDTYPE
//
// Update keyboard state
FUNCTION KeyHitUpdate:
GLOBAL gKeyDown[], gKeyState[]
LOCAL k, i
// First Time call
IF BOUNDS(gKeyDown[],0)=0
DIM gKeyDown[256]
DIM gKeyState[256]
ENDIF
// For each key
FOR i=0 TO 255
k = KEY(i)
// Key is pressed
IF k
IF gKeyDown[i]
gKeyState[i]=1
ELSE
gKeyDown[i]=1
gKeyState[i]=2
ENDIF
ELSE
// Key is not pressed
// Has key been pressed before?
IF gKeyDown[i]
gKeyDown[i] = 0
gKeyState[i] = -1
ELSE
gKeyState[i]=0
ENDIF
ENDIF
NEXT
ENDFUNCTION
// 0 = not pressed
// 2 = just pressed
// 1 = pressed
// -1 = release event
FUNCTION KeyHit: nkey
RETURN gKeyState[nkey]
ENDFUNCTION
///////////END KEYCODE FUNCTION//////////////////////////////////////////
FOR i=0 TO 255
k = KEY(i) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Problem here
// Key is pressed
IF k
IF gKeyDown[i]
gKeyState[i]=1
ELSE
gKeyDown[i]=1
gKeyState[i]=2
ENDIF
ELSE
That works fine for me, what happens if you comment out that line?
It turns out that I had another type with an initialisation "Global i as MyType". When I changed the 'i' to something else it didn't throw the error but a different one pointing to the end of the keyhit file saying out of bounds. :/
This is takig up too much devtime so I removed it. My game works fine now just have to find some other replacement for keyhit.
Keyhit is very easy to program yourself and you don't need that long list of keys in it. Just the key scancodes for the keys pressed which you hold in an array.
I'm not really very competant with programming yet so can you come up with a small example, please? :)
In this case all you need to do is replace the variable "i" with anything else, that you aren't using anywhere in your program.
In general one-letter variables are only used in small local loops like here, for i = 0 to 255. Don't use them in types because the compiler will mix them up with things like this even though the local variable should be totally separately from the global type.
Rename the variable in the KeyHitUpdate() function to anything else that you don't use in your program and it should work. Or rename the TYPEd variable you are using somewhere else to something longer.
Just to make it clear: If you make a type like this...
TYPE my_type
x; y
ENDTYPE
LOCAL my_types[] as my_type
...you are not allowed to use my_types as a local variable inside a function even though it should be local to the function because the compiler thinks of types globaly :-) However it's perfectly safe to use it as the same type as you declared earlier in the program.
So to make your life easier, don't use i, x or y as a type :-)
I have made sure there is nothing like that conflicting in code. Am a bit tired now so will try and tackle this in the morning.
Thanks for your help. :)
Well I figured it out. lol! I had a good few hours sleep and when I came back to the code I realised the brain fart I did.
It's all in the loop baby;
WHILE KEY(01) = FALSE
KeyHitUpdate()
MOUSESTATE MX,MY,MB1,MB2
IF DoGameOrMap = 0
ELSEIF DoGameOrMap = 1
MapEditor()
ENDIF
SHOWSCREEN
WEND
Basically keyhitupdate() is inside the loop. What I did was have the begining of the while loop: "While KeyHit(01) = 2" this is what was causing the OOB because keyhitupdate was not updating it.
I r teh wonderful noob. :D
:booze:
WHILE 1
// press "enter" - key down
IF KEY(28)=1; i1=i1+1;ENDIF;
// press "enter" - key hit
IF st=0;
IF KEY(28)=1; st=1; i2=i2+1;ENDIF;
ELSE;
IF KEY(28)=0; st=0;ENDIF;
ENDIF;
// text
PRINT "key hit: "+i2+" | key down: "+i1,10,100;
SHOWSCREEN;WEND
So, what does this keyhit program do? I'm trying to learn glbasic, I'd appreciate your kindness. :happy:
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).
Quote from: FutureCow 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?
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.
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.
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.
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
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.