Handling complex keyboard input

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Handling complex keyboard input

Post by rmcode »

I'm looking into input handling atm. As far as I understand the best way to go is using scancodes because they aren't dependent on the user's keyboard locale. Naturally there aren't scancodes for some keys like "?" or "A". What's the best practise to handle these?

I could use love.textinput to get the actual character produced by "lshift" + "1" for example, but I couldn't check for this character in anything like "love.keyboard.isDown" right?

I thought I could maybe save

Code: Select all

command = { keycombination = "lshift", "1", actualCharacter = "!" }
But how would I get the actualCharacter that's produced by lshift+1 on the users keyboard? :D

And before anybody says something like "you'd never use those keys in a game" you probably have never played a roguelike.
http://adomgb.sweb.cz/adomman-O.html
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Handling complex keyboard input

Post by grump »

Use textinput if you need the character generated by that input, and scancodes if you want positional input and don't care about the character.

Scancodes are raw positional data from the keyboard. You use them if you want input that is independent from the char generated by that key. For example, you use WASD for movement in FPS because their scancodes are arranged in the same way as the arrow keys, regardless of the keyboard layout. You don't care what character the generate.

You want to assign an action to "!", because of semantics. It has specific meaning, because it is "!", not because of the position of the key.

Attempting to translate scancodes into charcodes is madness. Don't do it.
Last edited by grump on Wed Nov 08, 2017 11:49 pm, edited 1 time in total.
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Handling complex keyboard input

Post by rmcode »

Attempting to translate scancodes into charcodes is madness. Don't do it.
I don't want to.
grump wrote: Wed Nov 08, 2017 11:44 pm Use textinput if you need the character generated by that input [...]
But how do I know which button combination produced that character? Or is there a way to use the produced character to check for "isDown" events.

Essentially I want to be able to say -> Player pressed A -> Player is holding A down -> Player released A.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Handling complex keyboard input

Post by grump »

rmcode wrote: Wed Nov 08, 2017 11:49 pm But how do I know which button combination produced that character?
You don't. What do you need that for? If you knew that Shift+1 generated !, what are you gonna do with that information?

If you want to know which keys are down, keep track of keys in love.keypressed/keyreleased. If you want to know which character was entered, use textinput. If you want to check a specific key, use love.keyboard.isDown or love.keyboard.isScancodeDown.
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Handling complex keyboard input

Post by rmcode »

Because

Code: Select all

love.keyboard.isDown( '!' )
will never return true and I need some way of checking wether that key is pressed. That's what this whole thread is about.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Handling complex keyboard input

Post by slime »

If you want to assign actions/inputs to combinations of keys, you should just present those key combinations to the user. "!" is only shift+1 on some keyboard layouts, so if you want to use shift+1 (or more accurately, shift + the key where '1' is on a US keyboard layout) you should just present it to the user instead of trying to resolve what the text input would be based on the key combination you want.

You should also present key codes (e.g. getKeyFromScancode) to the user rather than scancodes, generally. Scancodes are best used for internal logic.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Handling complex keyboard input

Post by grump »

rmcode wrote: Thu Nov 09, 2017 12:08 am I need some way of checking wether that key is pressed.
But ! is not a key. Keys have scancodes. There is no scancode for !. That's not how it works. You can only check for scancodes or combinations thereof, not for characters.

The example you have linked does not seem to rely on checking the key combination the way you want it either. It receives and processes character information and acts on that instead of checking each key individually.
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Handling complex keyboard input

Post by rmcode »

slime wrote: Thu Nov 09, 2017 12:45 am You should also present key codes (e.g. getKeyFromScancode) to the user rather than scancodes, generally. Scancodes are best used for internal logic.
Yeah, that's what I am doing at the moment with the keys that have scancodes.
If you want to assign actions/inputs to combinations of keys, you should just present those key combinations to the user. "!" is only shift+1 on some keyboard layouts, so if you want to use shift+1 (or more accurately, shift + the key where '1' is on a US keyboard layout) you should just present it to the user instead of trying to resolve what the text input would be based on the key combination you want.
Ok, I don't want to use key combinations. Logging "shift + whatever key" was just an idea how to approach it. I guess this means I should use scancodes for all "normal" keys and use "text.input"-characters for the rest?

So let's say I bind my help screen to the "?" key, a german keyboard would require "shift + ß" whereas an american keyboard would require "shift + /", but both would produce the "?" character in text.input, right?

Hm ... love.textinput basically just behaves like love.keypressed right? I mean there is no love.textinput released obviously. I mean I could do something like this

Code: Select all

function love.textinput(t)
    keys[t] = true
end
then clear that at the end of each frame. How often does textinput trigger if one would hold down the "@" key for example?

The example you have linked does not seem to rely on checking the key combination the way you want it either. It receives and processes character information and acts on that instead of checking each key individually.
I think we are talking about two different things entirely ^^ At least I'm not sure what you are trying to tell me here.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Handling complex keyboard input

Post by slime »

rmcode wrote: Thu Nov 09, 2017 1:48 am Ok, I don't want to use key combinations. Logging "shift + whatever key" was just an idea how to approach it. I guess this means I should use scancodes for all "normal" keys and use "text.input"-characters for the rest?

So let's say I bind my help screen to the "?" key, a german keyboard would require "shift + ß" whereas an american keyboard would require "shift + /", but both would produce the "?" character in text.input, right?
You should allow key combinations and assign a sensible default for the average user, and allow rebinding. There is no way to automatically map every layout's '?' key combination back to an actual key combination because that's not how keyboards expose things to the OS, or how the OS exposes things to apps, or how (bug-free) apps present themselves to users - it's a fundamentally broken approach to try. As grump said, keyboards do not send that information (there is no HID scancode for it).

rmcode wrote: Thu Nov 09, 2017 1:48 am Hm ... love.textinput basically just behaves like love.keypressed right?
No. There is no guarantee of a mapping between key presses and text input, or vice versa. There are multiple possible key combinations to get the same text input, and it's possible to enter text via other means than key presses to produce text input. There's a reason why they are separated.

rmcode wrote: Thu Nov 09, 2017 1:48 am then clear that at the end of each frame. How often does textinput trigger if one would hold down the "@" key for example?
An un-knowable amount of times determined by the OS and the user's OS settings.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Handling complex keyboard input

Post by grump »

rmcode wrote: Thu Nov 09, 2017 1:48 am Yeah, that's what I am doing at the moment with the keys that have scancodes.
Every key has a scancode. That's the definition of a key.
]Ok, I don't want to use key combinations.
Then you don't want to check for !, because that's the only way to do it. Because ! is not a key.
So let's say I bind my help screen to the "?" key
There is no such key. It does not exist.
I think we are talking about two different things entirely
No we don't, you just have trouble comprehending what I'm trying to tell you. No game in existence on this planet does what you're requesting, and the game you have linked to doesn't either.

Keys and characters are entirely different things. You keep conflating them. What you request is physically impossible, because you can't scan keys that do not exist. You need to decide if you want to check for keys or want to check for character input. There is no easy way to convert one into the other.
Post Reply

Who is online

Users browsing this forum: No registered users and 69 guests