Are we stuck with number codes when programming key input?

Previous topic - Next topic

Lord_TNK

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.

MrTAToad


dreamerman

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 Update: 20.04.2020

mentalthink

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

/.../




matchy

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?

Lord_TNK

Quote from: MrTAToad on 2012-Nov-28
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.

Quote from: matchy on 2012-Nov-28
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

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

Lord_TNK

Quote from: MrTAToad on 2012-Nov-29
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)?

Kitty Hello

I think about a way. Might add constants in a file in samples/common for this.

Lord_TNK

Quote from: Kitty Hello on 2012-Nov-29
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

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

Lord_TNK

Quote from: MrTAToad on 2012-Nov-30
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.

fuzzy70

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)

Lord_TNK

Quote from: fuzzy70 on 2012-Nov-30
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.

kanonet

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