Keyhit/Keydown???

Previous topic - Next topic

Amon

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! :)
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Moru

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 :-)

Code (glbasic) Select

// --------------------------------- //
// 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


Amon

Hi Moru!

Thanks for the code, it works perfectly. Will this ever make it as standard in the compiler?
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Amon

With the latest version of GLBasic (just updated) I get the following error in this code:

Code (glbasic) Select

"keyhitc.gbas"(201) error : wrong argument type : KEY, arg no: 1
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Moru

I don't have any troubles after updating, can you show a bit more of the code?

Amon

Here's the file:

Code (glbasic) Select


// 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//////////////////////////////////////////





Code (glbasic) Select

  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






- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Moru

That works fine for me, what happens if you comment out that line?

Amon

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.
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Moru

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.

Amon

I'm not really very competant with programming yet so can you come up with a small example, please? :)
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Moru

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 :-)

Amon

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. :)
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Amon

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;

Code (glbasic) Select

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






- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

com_1

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