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

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
I find I prefer to use the WASD setup for moving around in games*, but I'd like to actually type "W", "A", "S", and "D" on the function code instead of "30", "31", and "32", and... I keep forgetting what the code for "W" is.

Yeah, there is a way to look up the codes in the GLBasic SDK, which is convenient, but it would be even more convenient not to have to use those numbers (preferably not even set up pointers for them).

* Well I prefer gamepads even more, but I have to first learn how to set up ways for the player to map controls in the options.

EDIT: I have a program to help with listed on this post.
« Last Edit: 2012-Dec-04 by Lord_TNK »

MrTAToad

  • Guest
You could try INKEY$() !

Offline dreamerman

  • Global Moderator
  • Dr. Type
  • *******
  • Posts: 288
    • View Profile
    • my personal website
Answer is same as for Your other topic - about sprites..
You can replace function parameters with variables so:
Code: GLBasic [Select]
CONSTANT mykey_W% = 17
Global mykey_A% = 30

if Key(mykey_W%)
''something
endif
if Key(mykey_A%)
''something
endif
 
Check my source code editor for GLBasic - link

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3366
  • Integrated Brain
    • View Profile
Code: GLBasic [Select]
You can use in example

global key_L      = key(203)
global key_R      = key(205)
global key_U      = key(208)
global key_D      = key(200)
global key_Fire   =  key(57)

in the loop
/.../
if key_L             // In the if coomands, if you don't put nothing GLbasic want say "true" , you can put key_L=false or key_L it's
                        // the same... obviously, if you need check like false put key_L=false
   do something
endif
 
 and so on....

/.../

 

« Last Edit: 2012-Nov-28 by mentalthink »

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1542
    • View Profile
Why is such as inconvenience to look-up or store global constants for the key-code values because they would only be used once in code?

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
You could try INKEY$() !
Is that for mapping a command to a specific key, or just for pressing any key? The info on the tutorials make it look like the latter, while I'm looking for the former.

Why is such as inconvenience to look-up or store global constants for the key-code values because they would only be used once in code?
For readability, for one thing. GLBasic is a lot more readable than C++ for the most part, but having to enter a code for keys instead of "KEY_X" is not one of those things.

MrTAToad

  • Guest
If you want the latter, then you will need to check all scan codes unfortunately.

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
If you want the latter, then you will need to check all scan codes unfortunately.
What I mean is a way to write a code so I wanted the program to read me pressing "A", I could actually write "A" as the variable, not "30".

Can that be done with GLBasic, preferably without having to use a pointer (which would mean I'd still have to use the key code at some point)?

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10708
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
I think about a way. Might add constants in a file in samples/common for this.

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
I think about a way. Might add constants in a file in samples/common for this.
Correct me if I'm wrong, but do you mean a file that just provides pointers for all the key codes, and a program just has to call that file?

MrTAToad

  • Guest
Some sort of scan code to ASCII (or text is needed).  Its not hard (just need an array of strings) - the problem is describing things volume control, mail programs etc etc

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Some sort of scan code to ASCII (or text is needed).  Its not hard (just need an array of strings) - the problem is describing things volume control, mail programs etc etc
Okay, I'm honestly lost there. Could you spell out what that means?

Also, I'm correcting a mistake on a previous post. I claimed C++ let you type in key names, and forgot that was a feature of Allegro for C++, not C++ by itself.

Offline fuzzy70

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 828
  • Look left, Look right, LOOK OUT!!
    • View Profile
Take a look at this thread http://www.glbasic.com/forum/index.php?topic=8734.0, Siatek's very useful input program. If it is not exactly what you are after then at least it will save you typing out all the keycodes & naming them  :D

Also you mentioning pointers confuses me a bit, do you mean variables?. As far as I recall a "Pointer" points to the contents or the location of a variable, whereas a variable is just a name assigned to a value you want to store/modify (both examples simplified as could easily fill a page with a full definition of both).

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Take a look at this thread http://www.glbasic.com/forum/index.php?topic=8734.0, Siatek's very useful input program. If it is not exactly what you are after then at least it will save you typing out all the keycodes & naming them  :D (1)

Also you mentioning pointers confuses me a bit, do you mean variables?. As far as I recall a "Pointer" points to the contents or the location of a variable, whereas a variable is just a name assigned to a value you want to store/modify (both examples simplified as could easily fill a page with a full definition of both). (2)

Lee
1. I'm looking at that link. I'll see if it works.

2.  Well perhaps "pointer" isn't the right term then. It's whatever term would apply to seeking an alternate name for this.
« Last Edit: 2012-Nov-30 by Lord_TNK »

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
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
Of cause you could solve this when you know the keyboardlayout, but thats nothing that you can do in native GLB.

Thats why you have ascii codes and scancodes and it would be a really bad idea to drop one of them, its horses for causes!


Generally i dont really understand the problem in typing a few numbers (in a good game you wont type them in code at all, but read them from a ini, cause you allow users to change keys), if you have problems to work with key codes (at least big enough problem to start a thread about it), how do you code, what do you do, when you hit a real problem?
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64