Author Topic: Are we stuck with number codes when programming key input?  (Read 8894 times)

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
No problem the helpful community is one of the strong points of this language, so im happy when i can help too.

My keydef has ini for german keyboard layout and one for us. So just use the us one instead of the german and you will need to do less tweaking. Btw i just realised that there is a huge overhead in my code so i will do a rewrite in next weeks.
Well get back to us when you do, because I think we all prefer simpler code for any task.

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Okay, I figured out how to use "INKEY$()" to make a program accept w, s, a, and d. I just can't figure out how to make the program accept their input in a function. And the help for the SDK really doesn't tell me. Heck, the program won't even compile (as to most of the program samples in the tutorial, but that's for another thread).

Here is the program code. I want to replace the inputs for KEY (17), (30), (31), and (32) with the letters, and "spacebar" for (57).

Code: GLBasic [Select]
SETCURRENTDIR("Media") // go to media files

SETTRANSPARENCY RGB (244, 124, 0)

LIMITFPS 60

LOADSPRITE "whatever.bmp", 5 //Any picture will do. I used a 32x32 png.
GLOBAL shield% = 5

GLOBAL spritex = 128
GLOBAL spritey = 128
GLOBAL spritespeed = 1
GLOBAL w$ = INKEY$()
GLOBAL a$ = INKEY$()
GLOBAL s$ = INKEY$()
GLOBAL d$ = INKEY$()
GLOBAL spacebar$ = INKEY$()

WHILE KEY (01) = FALSE

        MovePlayer()
        DrawScreen()

WEND

FUNCTION MovePlayer:

LOCAL dirx, diry

        IF KEY (17)
                diry = -1
        ENDIF

        IF KEY (30)
                dirx = -1
        ENDIF

        IF KEY (31)
                diry = 1
        ENDIF

        IF KEY (32)
                dirx = 1
        ENDIF

        spritex = spritex + (spritespeed * dirx)
        spritey = spritey + (spritespeed * diry)

        //turns the spacebar into a dash button
        IF KEY (57) THEN spritespeed=3
        IF NOT KEY (57) THEN spritespeed=1

        IF spritex < 0 THEN spritex = 0
        IF spritex > 608 THEN spritex = 608
        IF spritey < 0 THEN spritey = 0
        IF spritey > 448 THEN spritey = 448

ENDFUNCTION

FUNCTION DrawScreen:

        DRAWSPRITE shield, spritex, spritey

        SHOWSCREEN

ENDFUNCTION
« Last Edit: 2012-Dec-04 by Lord_TNK »

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1621
  • Particle Hawk
    • View Profile
you will need to define a huge list which is slow.
i think what youre trying to do is much harder than it sounds...or time consuming rather.
have you tried KEYCODE tool?
you need to map out your key codes.....the keyboard sends these...not letters.


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

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
you will need to define a huge list which is slow.
i think what youre trying to do is much harder than it sounds...or time consuming rather.
have you tried KEYCODE tool?
you need to map out your key codes.....the keyboard sends these...not letters.
I know about the keycode tool. I just think it's less confusing to type out the name of a key than try to remember the code for all of them.

Plus I can just post a list of "INKEY$()" strings and use that as an extra file for all my programs, so it would just be work right now, not later.

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1766
    • View Profile
    • Homepage
I only see a couple of problems for that approach.









Look on these pictures. with some of these keyboards your game would be controlled with keys all over the keyboard. This is exactly why all computers uses scan-codes to control what keys are pressed instead of a letter. This is just they way things work when programming low-level things.

It starts with 1 in the upper left and then goes straight over the whole keyboard (with some exceptions here too ofcourse), row after row. This way the keys will be on the same location for almost all keyboards. You will still get problems in some areas but this is why you always should have a way for the user to change keys themselves.
So I'm affraid the easiest way is to just get used to the scancode way like all other gameprogrammers have been the last 40 years :-) Well obviously not games like nethack that has about three-four functions for each button on the keyboard...

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
1. I didn't realize that keycodes were the same, even if the names of the keys were not.

2. I'd still like to know how to do it anyway, since that would mean I know more about the language and programming.

3. Even then, the games I make will have button customization, since that will just be convenient for the players.

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
1. I didn't realize that keycodes were the same, even if the names of the keys were not.
I did tell you about this on the 1st page of this thread. :P
Problem with scancode vs. keycaption is, that you dont know what keycaption which scan code has. For example:
On us/uk keyboard key(44)=z and key(21)=y
But on ger keyboard key(44)=y and key(21)=z

Sorry. :-[
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
1. I didn't realize that keycodes were the same, even if the names of the keys were not.
I did tell you about this on the 1st page of this thread. :P
Problem with scancode vs. keycaption is, that you dont know what keycaption which scan code has. For example:
On us/uk keyboard key(44)=z and key(21)=y
But on ger keyboard key(44)=y and key(21)=z

Sorry. :-[
Well I honestly didn't quite know what that meant a few days ago. Still learning this, but also glad you're all here to help.