Detected the presence of a physical keyboard

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.
Post Reply
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Detected the presence of a physical keyboard

Post by Bigfoot71 »

Hi everyone :D

I was wondering if there is a way around Löve2D to detect the presence of a physical keyboard.

Because I am currently making a game that is PC / Android compatible and I am making a menu that displays the controls, so if a Gamepad is active it displays it otherwise it displays the keys on the keyboard but if we are on a phone I would have liked to display touch controls, so is there a way to do this?

Knowing that I thought about the fact that we can connect a keyboard to Android (yes, I try to think of everything, even the improbable).
Currently I therefore use love.keyboard.hasScreenKeyboard to differentiate but it will not work as expected if you connect a physical keyboard to your tablet for example.
My avatar code for the curious :D V1, V2, V3.
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: Detected the presence of a physical keyboard

Post by GVovkiv »

Well, not something that relevant to question, but, still:
You can also connect gamepads to android, don't forget about this fact. And also i strongly encourage (if you still didn't) you to add option to force game use specific input controls hints, for example like this:

Code: Select all

if config.detectInputDeviceAutomaticaly then:
		inputDevice = detectDevice()
	else
		inputDevice = config.manuallySelectedInputDevice
	end
Because sometimes detecting might work not very nice, especially in scenarios when some compatibility layers in use (for example, wine on linux for windows game) or system were game run fails to detect device properly (for example, games might fail to detect Playstation gamepads, despite game having icons and support for them. The best example would be linux version of Hollow Knight. I was playing for at least 150 hours in linux version on dualshock, with xbox prompts, only to later found out that windows version via proton detected gamepad properly. And since game doesn't has option to change gamepad type, i was unable to do anything with it; Also another example: some games prefer gamepad over keynoard+mouse, so if you have connected gamepad, they will show gamepad prompts until i disconnect gamepad. And that's annoying, since i have egamepad connected 24/7 and sometimes i want keyboard, but game might disagree with me, until i disable it)
So, until someone come up with solution (it seems that it possible to detect physical keyboard on android with kotlin and java, but no examples in c or lua; but im not sure about ios) i think it will be easier and more reliable to just implement option to change current input device manually, no matter what os.
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Detected the presence of a physical keyboard

Post by Bigfoot71 »

Thank you for your answer, indeed it seems to be the best solution.

I came across the same observation with multilingual support, I wanted to add translations to my game with a system to detect the system language but after seeing all the problems it added for Android I finally added a menu of selection in the settings as well as at the start if the language was not well detected.

I think I'm going to do the same, i.e. detect the controller first, if not check whether we have on-screen keyboard support or not (to try to differentiate touch and physical keyboard), and add a menu of selection of additional controls.

All these automation stories are originally to save time by not making a selection menu but in the end we waste more time making it work on all platforms, which is counterproductive...

Thanks again, I'll make those changes! :D
My avatar code for the curious :D V1, V2, V3.
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: Detected the presence of a physical keyboard

Post by GVovkiv »

Bigfoot71 wrote: Sat Mar 25, 2023 1:43 pm All these automation stories are originally to save time by not making a selection menu but in the end we waste more time making it work on all platforms, which is counterproductive...
I think here more applies principle, that no matter how good your system that automatically detects something (like in your case input device) it never will be good enough. Like i pointed out, connected gamepad doesn't automatically means that i want play with it, or if i play on android device with connected keyboard it doesn't mean that i want 100% use it instead of touchscreen. So if game force to play with gamepad if it connected, it's just annoying.

Same actually with languages: while you can get current system locale and use it to set language for game, you should never assume that user want this exact language in your game, even if you support it. Lets take another example:

I has linux system that set to ukranian locale. And your game, lets say, has 4 supported language: polish, russian, english and maybe french. I ran your game and since your game doesn't support my language of system, game will fallback (i hope it will) to english instead. Since game might not have options for language, what i can do now? Hack some configs if game has one (that's sucks) or change entire os language just for 1 game (that's suck more).

Or i use linux with english language, but for games and other content i prefer, lets say, polish, because i understand it better on fly (e.g, when watching movies, videos, etc), while for not heavy text scenarions i prefer english. Now game detects english on system, and since there exist english translation, it will be used. What now? How change language?

Or yet another example:
I use system with ukrainian language and you has ukraninan translation, but it's turns out to be bad. Bad translation itself, worse fonts, worse dub, etc. How to change language if this one that i know sucks?
Kinda offtopic, but: Situations like this happens quite often for games that translated to russian or ukrainian. In case of russian, translations often sucks (or language itself, but i'm just biased here) or just worse in all aspects, including dub quality. And in ukrainian case, in 99.5% of cases ukranian dub not exist for games.

So, yeah, making automatic decisions for player and making then permanent is kinda... bad. So, use automatic detections for initial game setup (or when user explicitly told your game to do so, like in my example with ui input detecting) and then add option to change it.
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Re: Detected the presence of a physical keyboard

Post by BrotSagtMist »

I second that, auto detection is a pain, worse if it hides options that it thought _you dont need_

Anyway the question still is interesting and there is little documentation.
Can we get if there is a keyboard?
What does love.keyboard.hasScreenKeyboard( ) report for example?
Would it be triggered only on android or does it include any virtual input method?
One could get all kinds of returns if you have a tablet powered by debian for example i guess.
obey
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: Detected the presence of a physical keyboard

Post by GVovkiv »

BrotSagtMist wrote: Sat Mar 25, 2023 3:00 pm Anyway the question still is interesting and there is little documentation.
Can we get if there is a keyboard?
What does love.keyboard.hasScreenKeyboard( ) report for example?
Would it be triggered only on android or does it include any virtual input method?
One could get all kinds of returns if you have a tablet powered by debian for example i guess.
As for keyboard, i'm not sure. Aren't, at least in windows and linux, system always has 1 keyboard reserved? So even if you disconnect keyboard from system and connect another one, system will continue to work as nothing happened? That's probably explain why there is no any game or program that will somehow react on keyboard disconnect (at least, i know none of games or programs that doing something on keyboard disconnect and reconnecting). Same probably with mouse. Only gamepads is different in input.
Can we get if there is a keyboard?
If feels, that the most easy way is at start of game make input field that will wait for any keyboard input and if it user don't have keyboard, then they will press "no, i don't have keyboard" and then do something with this data...
Or similarly: defaults to keyboard prompts so if user has keyboard they will start use it and if they didn't then use other input method, which game will switch to.
Or as some games do, they has *classic* "Push start button" screen with "Press A (with icon of A key on keyboard) or A (A on xbox gamepad) or Click/Touch" so user know that they preferred method is supported an depending on what they used to start game, and then save this choice to save file and if they later want to change it, they will go to options. Still feels more like workaround, but not bad one.
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Detected the presence of a physical keyboard

Post by Bigfoot71 »

BrotSagtMist wrote: Sat Mar 25, 2023 3:00 pm Would it be triggered only on android or does it include any virtual input method?
Interesting I asked myself the same question but it is true that the wiki is empty on this subject...
I tried with the Onboard virtual keyboard on Linux Mint and the function still returns false, should try on Windows.

After that I don't know anyone who plays video games with a virtual keyboard, even if it returns true it shouldn't be a problem ^^
Conclusion, even if we make an automatic detection, left the final choice to the user!

Note: I had tried several things using FFI like with SDL2 functions like SDL_HasKeyboardFocus, although I then figured it might return true even without a keyboard. I also saw that there was a function SDL_GetNumKeyboards but it only seemed to be available on SDL 1.x
My avatar code for the curious :D V1, V2, V3.
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Re: Detected the presence of a physical keyboard

Post by BrotSagtMist »

Input methods are a mess anyway. Löve doesnt even detect my keyboard fully because of a bug in SDL ignoring parts of custom keyboard layouts.
This is the thing why it interests me in the first place, People using my layout try to solve this in different ways: just setting the layout leaves this bug so there are attempts of disconnecting the original driver and shove a virtual translation layer in between, questionable in how far this then differs from a virtual keyboard.
Also i do have said debian tablet, i need to research this someday.

Now if we are talking about game inputs i do wonder why this matters. Just have all the input methods active all the time and channel them all to the same target, so pushing the keyboard arrow key to right triggers the same events as pushing a D pad right.
And for displaying the layout you can then simply use whichever method was used last or to click on "show".
So after the menu entry is awoken do a love.event.wait() which would then return either a keyboard keyup event or a joypad event.
obey
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Detected the presence of a physical keyboard

Post by Bigfoot71 »

I didn't want to go into all the details but that's exactly how I do for my game, the keyboard/gamepad/touch screen inputs call for the same functions, if the gamepad is plugged in it can be used just like the keyboard in at the same time, everything is handled in the callback functions so that's no problem.

My problem is just in what type of control to display by default in the settings (screen that displays the game controls).

My solution is therefore to display the default keyboard (or the gamepad if it is detected when the game is started) and if a key is pressed on the gamepad then the gamepad controls are displayed. If a keyboard key is pressed while another type of control is displayed then it displays the keyboard controls.

It was the simplest solution for me, I could have made a switch between the types of controls with arrows/buttons but I put animations when pressing the keys just to look pretty and I wanted them guard.
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 52 guests