Detect keyboard

Hello everybody,

I want to detect if a keyboard is present or not. The user should see a hint that no keyboard is installed.
I am using SDL2 2.26.5.
Is there a way (API call) to determine if a keyboard is present.or not?

Thanks in advance
Mik

As far as I know, there’s no way to do this with the SDL API — there’s no way to check how many keyboards are available (they are not enumerable like joysticks), and there are no events generated when the keyboard is plugged and unplugged (and the same for a mouse). There is always a keyboard on desktop platforms and modern operating systems — if not physical, then virtual (on-screen keyboard).

Thank you very much for your reply.
Meanwhile I found a SDL ‘trunk’ of the user ‘hercules’ with the functions
int SDL_GetNumKeyboards (void)
and
int SDL_SelectKeyboard (int index)
at Simple DirectMedia Layer: /Users/hercules/trunk/SDL/include/SDL_keyboard.h File Reference

But it seems that these functions are not longer available.
Best regards
Mik

This documentation was created in 2009, it is obsolete. The official documentation for the SDL2 and SDL3 is on these sites — SDL2/FrontPage - SDL Wiki and SDL3/FrontPage - SDL Wiki.

Support for multiple keyboards and mice has been planned for a long time, but AFAIK remains a plan for now. So for now it’s not possible to do what you need with the SDL API.

1 Like

Rather than detect the keyboard, you could detect if the user can press a key on the keyboard. Think about many games that support multiple gamepads. The user isn’t prompted to pick their gamepad, the game just asks the user to press a button. In the same way, just ask the user to press enter. If they can, they’ve got a keyboard. The issue is a keyboard can maybe be many things. Some gamepads like the Xbox 360 can have a keyboard attachments, some users with disabilities may use a virtual on-screen keyboard, and there are probably may l many other types I’m not considering. The best way to detect a keyboard is waiting for the user to use it.

1 Like