Are we stuck with number codes when programming key input?

Previous topic - Next topic

MrTAToad

There is an example project in _Projects that may be what you are looking for - it has a scancode to text array.  Unfortunately it needs a bit of a tweek to slow things down.

Lord_TNK

Quote from: MrTAToad on 2012-Nov-30
There is an example project in _Projects that may be what you are looking for - it has a scancode to text array.  Unfortunately it needs a bit of a tweek to slow things down.
Do you mean as part of the the samples that come with the SDK download?

matchy

A software keyboard may be another thing because it will at least be the same array for all platforms.

Lord_TNK

Quote from: matchy on 2012-Nov-30
A software keyboard may be another thing because it will at least be the same array for all platforms.
Would those integrate with the program code somehow?

MrTAToad

Quote from: Lord_TNK on 2012-Nov-30
Quote from: MrTAToad on 2012-Nov-30
There is an example project in _Projects that may be what you are looking for - it has a scancode to text array.  Unfortunately it needs a bit of a tweek to slow things down.
Do you mean as part of the the samples that come with the SDK download?
Thats right

Lord_TNK

Quote from: MrTAToad on 2012-Dec-01
Quote from: Lord_TNK on 2012-Nov-30
Quote from: MrTAToad on 2012-Nov-30
There is an example project in _Projects that may be what you are looking for - it has a scancode to text array.  Unfortunately it needs a bit of a tweek to slow things down.
Do you mean as part of the the samples that come with the SDK download?
Thats right
It's the one in the folder "KeydefMenu", or another one?


Lord_TNK

Quote from: MrTAToad on 2012-Dec-01
Thats the one...
Okay, that thing. It's just a game, and the code still uses key codes. So it's not what I had in mind.

EDIT: Although there's something in the "Common" folder. I'm checking that out.

Hemlos

Quote from: kanonet on 2012-Nov-30
(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)

Thats actually a great idea even in non-game programs also.
And store builtin key definitions into an array.
Bing ChatGpt is pretty smart :O

Lord_TNK

On that note, is there a good tutorial on how ini files work? Because the help files aren't really clear.

kanonet

The help file is not clear? Ok i dont know about the English one, but the sample in the German help file shows everything that is needed and i guess in English its the same? Anyway, the inis are really easy to use:
Code (glbasic) Select
// An ini looks like this:

[section1]
key1=value1

value1 is the information that you want to store there, section and key1 are the the information that you need to find that value when you load it. A ini file can contain as many sections as you want, but every section needs a unique name (in the same ini file). Each section can have as many keys as you want, but each key has to be unique in the same section. Each key can be empty or has exactly one value. Everything behind the '=' is the value, it can be numbers strings,... but maybe its better to avoid escape codes (not sure).
Code (glbasic) Select
// write to ini:
KILLFILE "test.ini"  // just in case there is an old ini with this name -> delete it
INIOPEN "test.ini"  // open the ini so we can read/write it

LOCAL attackkey% = 57  // lets say you want to store your attack key (in my sample its 57 = space bar)
// store your value in the ini, by telling which section and key you want it. you need to remember that section+key if you want to reload it later
INIPUT "section1", "key1", attackkey  // you can do as many INIPUT now as you want; btw it want value to be a string, but if you input a number it will automatically convert it for you

INIOPEN ""  // finally stop working on this ini, write it to HDD and close it

After doing this your ini test.ini will look like this:
Code (glbasic) Select
[section1]
key1=57

We can now read it into your program and use it:
Code (glbasic) Select
// read ini:
INIOPEN "test.ini"  // open the ini so we can read it

LOCAL attackkey%
// now read your key, you need to know from what section and key you want to read (and you can add a parameter what value you want to return if the section or key not found)
attackkey = INIGET$("section1", "key1", 28) // it returns an string, but it automatically converts it into a integer if you do it like this
INIOPEN ""  // again close the ini when you have read all the values that you want

// later in your program use the value that you just have read:
IF KEY(attackkey) THEN DoSomething()

Its easy, right?


Btw. I expanded the keydef sample to work with mouse/keyboard/joystick, work with primary and secondary key, support displaying the names of the defined keys (for us and ger keyboard layout) and being encapsulated and easy to use, maybe you want to have a look at this:
http://www.glbasic.com/forum/index.php?topic=5695
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Lord_TNK

@kanonet: So let me see if I got this right.

The first sample you used is a template for an ini file, or at least an ini file for using pointers and alternate commands.

The second is how you create an ini file. I don't think the help tutorials tell that, or at least not in a way as clear as you stated. So I guess a program includes that as a function to make such files when needed?

The third is what the ini file is according to the template. Is the [section1] thing to note different sections in an ini file to call, or is that just a variable name you chose?

And the fourth is to read it, and I assume a full program will include functions for both making them and reading them.

kanonet

yes 1st is just to show you how an ini file looks like. [section1] just shows you that you in a section, you can have multiple of them, most times to keep thinks easier to organise/read. Example, lets say you write all your game settings into one file:
Code (glbasic) Select
[options]
screenx=640
screeny=480
fullscreen=1

[keys]
attack=57
forward=200
backward=208

So you dont need to hardcode this, you can easily read/write it to ini, and the player can change it (in ini, or in options menu if you code one). You need to have a section, even if your file just contains one. For more information about ini files check this: http://en.wikipedia.org/wiki/INI_file

Correct 2nd is a tiny example how you could write an ini, 3rd is how this ini will look like, that my sample produces and 4th is a short sample how to read and use this particular ini. full program will at least need a read function, but if you allow the user to change keys in a options menu, you may want to write them back in the ini (so he does not to change keys after next restart again), so you will also need a write function. Its really easy, just play around with the code and write/read some fun nonsence ini files, just for practice. The best way to learn coding is just to test everything on your own, you will make mistakes (sometimes) but you will learn from them and dont forget what you have learned.

Btw if you have a look at that keydeftingy that i linked at the end of my last post, its designed to let the user select controls and read/write them to ini (you will have to stick it into your general ini saving routine).
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Lord_TNK

Thanks for the help. Now I would use the file you linked, but I have a US keyboard, so I'll have to do a lot of tweaking anyway.

Also, I'm not afraid to experiment (there are a few things I'm trying to do with my programs right now), just that I want to know what's even possible with this language, and want to avoid screwing up my system as much as possible.

kanonet

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.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64