Any way to query if a mouse or keyboard is attached to a system

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
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Any way to query if a mouse or keyboard is attached to a system

Post by Pangit »

So I can check if a joystick or gamepad is connected to the system with love. But I was thinking, is there some way of doing this for the keyboard and mouse?

I guess its possible to use system commands to query this but that is platform dependant.

Like for example android. Odds are there is going to be nothing but touch functionality.

What I am doing is checking if a device is connected and putting a little icon in the corner of the screen, it would also be used to enable/disable parts of the configuration menu (why have users mess with configuration of a device that is not connected to the system..)

It would also be useful as I could modify the behaviour of the program based on the available inputs and the preferred input device in game.

One partial solution is when there is input from the device use that to set the flag, but that would mean the program would not know the state of the input devices until the player used the device. What I want to do is peek at the polling of the device.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Any way to query if a mouse or keyboard is attached to a system

Post by airstruck »

Pangit wrote:I guess its possible to use system commands to query this but that is platform dependant.
Looks like you just answered your own question. Nothing in Love or SDL detects when or whether keyboards or mice are attached or removed. If you really want to you might be able to find a cross-platform library that does it; this looks somewhat promising at a glance. I'd just assume there's never a keyboard or mouse on mobile and always a keyboard and mouse on desktop for now and worry about this down the road if you still care about it at that point.
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Re: Any way to query if a mouse or keyboard is attached to a system

Post by Pangit »

Fair enough like your idea, I can do it through the system on linux but I am not sure how its done in windows or android or osx..

I like the simpler approach of probing the players system, assuming there was a straight forward way to figure out if love was running on a particular OS then you could just use that to within a high degree of accuracy figure if there is a keyboard/mouse or not and go from there. But I am guessing that would also be a series of tests using the system library in lua.
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Any way to query if a mouse or keyboard is attached to a system

Post by zorg »

[wiki]love.system.getOS[/wiki]
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Re: Any way to query if a mouse or keyboard is attached to a system

Post by Pangit »

Excellent. Thanks for that zorg. :ultrahappy:

That library is great, even had the next thing I was thinking to implement (power consumption and battery..)

love.system.getPowerInfo

For the curious...

Here is the general idea of how I did this, if you want to see the full working example check out my projects and demo thread (a little later tonight..)
osdetection.jpg
osdetection.jpg (130.99 KiB) Viewed 2287 times
This is an example of what can be done - for the visualisation impaired. A picture is worth a thousand words. :joker: The thing you want to look at is the icon bar along the top right of the screenshot. Your notice that I have a keyboard, mouse, joystick (well its actually a non-supported gamepad.).

Because the os was determined to be Linux, this would look identical for systems with Windows or OS X. Were I to be running iOS or Android then you would not see the mouse or the keyboard icon, or the joystick or gamepad.. didn't find a touch screen icon to use yet. but you get the idea. Its possible to set that as a flag also you would just add it to the iconflag table.

so first I set up my iconflags table this is just a table of all my flags that I use in the program, it lives near the start of the file.

Code: Select all

iconflags = { 
    nosound = false,
    issound = true,
    loading = false,
    saving = false,
    problem = false,
    keyboard = false,
    mouse = false,
    gamepad = false,
    joystick = false,
    touch = false
}
In love.load() you want to make sure you load the assets you are going to use later we add the following.
Remember that your directory and file names might be different to mine. Adjust as required. You can change the variables (icon_keyboard_small ect..) to whatever fits your needs. Remember if you decide to change the name of the function osPeek() you need to adjust it here also.

Code: Select all

    osPeek()

    icon_keyboard_small = love.graphics.newImage("/assets/keyboard_small.png")
    icon_mouse_small = love.graphics.newImage("/assets/mouse_small.png")
    icon_gamepad_small = love.graphics.newImage("/assets/gamepad_small.png")
    icon_joystick_small = love.graphics.newImage("/assets/joystick_small.png")
    icon_touch_small = love.graphics.newImage("/assets/touch_small.png")
This next bit is the meat and potatoes of the functionality... I split it off into its own function. There is nothing magical about the name I chose - you can of course adjust to your needs.

Code: Select all

-- This helps us guess if the player has access to a keyboard and mouse, we do this my taking an educated guess based on the OS that 
-- we have detected and go from there. 
-- OS X, Windows, Linux We can be fairly confident there will be a keyboard and mouse present. 
-- For Android & iOS we can be fairly sure there is not going to be a keyboard and mouse, if there is one the player could always set 
-- the option in the configuration screen manually later. But for 99% of players who are using Android & iOS platforms they are going
-- to be using touch interfaces.

function osPeek()
    osString = love.system.getOS()
    if osString == "OS X" then
        iconflags.keyboard = true
        icon_mouse = true
    elseif osString == "Windows" then
        iconflags.keyboard = true
        iconflags.mouse = true
    elseif osString == "Linux" then
        iconflags.keyboard = true
        iconflags.mouse = true
    elseif osString == "Android" then
        iconflags.keyboard = false
        iconflags.mouse = false
        iconflags.touch = true
        
    elseif osString == "iOS" then
        iconflags.keyboard = false
        iconflags.mouse = false
        iconflags.touch = true
    end
end
You might be thinking - I could just lump the phone/tablet and non phone/tablet into an if/ifelse conditional and leave it at that. However - you want to keep the os detection as is - knowing the os can help in other ways. You might want to render the menus, interface ect in various different ways dependent on what os is used. Like for example apple has conventions for displays that does not apply to windows and vice versa.

It has even greater applications as you can target things likes advertising ect based on platform specific information. It would also be a good secondary verification for things like licence keys ect... lots of potential for such a simple check.
So in your function where you draw your icons You add the following.

Code: Select all

    if iconflags.keyboard == true then
        love.graphics.draw(icon_keyboard_small, 0, 0)
    end

    if iconflags.mouse == true then
        love.graphics.draw(icon_mouse_small, 0, 0)
    end

    if iconflags.joystick == true then
        love.graphics.draw(icon_joystick_small, 0, 0)
    end

    if iconflags.gamepad == true then
        love.graphics.draw(icon_gamepad_small, 0, 0)
    end
    
    if iconflags.touch == true then
    	love.graphics.draw(icon_touch_small, 0, 0)
    end
Finally you add the following to your joystick function to complete the input device detection. as game pad is built upon joystick it makes sense to put it here. remember that you might have defined joysticks as something else its dependant on how you wrote the function. make sure its the same as the joystick object you used.

Code: Select all


local isgamepad = joysticks[1]:isGamepad( )

    if joysticks ~= nil then
        iconflags.joystick = true
    end

    if isgamepad == true then
        iconflags.gamepad = true
    end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 13 guests