Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a signal that is emitted when the keyboard is disconnected #6689

Open
pavlexander opened this issue Apr 13, 2023 · 5 comments
Open

Add a signal that is emitted when the keyboard is disconnected #6689

pavlexander opened this issue Apr 13, 2023 · 5 comments

Comments

@pavlexander
Copy link

Describe the project you are working on

local coop game with monster swarming elements

Describe the problem or limitation you are having in your project

when player disconnects I would like to pause the game in order to avoid player dying in-game due to waves of monsters

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add an event to detect keyboard disconnection

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

following is the code to detect joystick disconnection in C# in godot 4.0

    public override void _Ready()
    {
        Input.JoyConnectionChanged += JoyConnectionChanged;
    }

    private void JoyConnectionChanged(long device, bool connected)
    {
        GD.Print("Device " + device + " is connected? " + connected);
    }

Similarly I would like to be able to monitor keyboard-like devices disconnection events:

        Input.KeyboardConnectionChanged += KeyboardConnectionChanged;

alternatively it would be nice to have a single all-around method for device connection/disconnection purposes with a device type enum as parameter

        Input.DeviceConnectionChanged+= DeviceConnectionChanged;

    private void DeviceConnectionChanged(long device, bool connected, DeviceTypeEnum deviceType)
    {
        GD.Print("Device " + device + " is connected? " + connected);
    }

enum DeviceTypeEnum{
Controller,
Keyboard,
Mouse,
Headset,
Other
}

the issue with last proposed solution, however, is that currently first connected controller has an device ID of 0 and so is the keyboard. If we get a disconnection event on device ID 0 then it could be either of the two, so we need an additional parameter to further understand what kind of device was disconnected. In example above I added an enum DeviceTypeEnum, but it might not be possible to do it like that due to sheer number of all kinds of devices available on the market, so some kind of alternative solution might be preferred.

If this enhancement will not be used often, can it be worked around with a few lines of script?

can't be worked around

Is there a reason why this should be core and not an add-on in the asset library?

This should be a core feature of a Godot. Nowadays, people tend to use wireless devices, including wireless keyboards. If a keyboard gets disconnected for whatever reason (battery died or cable damage) - it can cause the player to experience some unpleasant consequences in a fast-paced game.

I think it's a very good use-case and reason for adding support for disconnect events for keyboards.

@AThousandShips
Copy link
Member

AThousandShips commented Apr 13, 2023

I'm not sure how simple this is, and I don't think you're guaranteed to get a disconnect signal from a wireless keyboard running out of battery, nor getting it fast (within several seconds, for power efficiency reasons)

My assumption is that a lot of wireless keyboards don't constantly send status signals to conserve power, only sending them when updating status (keys pressed, etc.) and there's likely a poll from the receiver to check status periodically, but to conserve power (as transmitting wireless isn't very cheap power wise) it probably won't do this often, as there's usually no reason, it also prevents random disconnect events in case there's a brief loss of contact (due to interference etc.), avoiding massive annoyance in such cases

The usecase would be the keyboard dying and you needing immediate update in the game, and I don't think this can be expected on a lot of hardware, though I might be wrong with modern efficiency levels etc.

This would likely be very unreliable and depend on hardware so would create a lot of confusion if someone expects this to work on their hardware, I think this could only reliably work on wired keyboards, where there's no real reason to have this

It should be possible to do on Linux (when the hardware supports it), but not so sure about Windows

@Calinou
Copy link
Member

Calinou commented Apr 13, 2023

Keyboards do not use a gamepad-style API for (dis)connecting, so this would require listening for USB event changes which is more difficult.

@Calinou Calinou changed the title tracking keyboard disconnection event Add a signal that is emitted when the keyboard is disconnected Apr 13, 2023
@pavlexander
Copy link
Author

I think this is also problematic because PC could have tens of keyboards connected at the same time. But for Windows it's irrelevant as all of them are going to overlap one with another. Pressing W on keyboard 1 is equivalent to clicking W on any of them. So if we add disconnection event it's not going to mean a thing if the rest of the keyboards are still connected and are usable from system's POV.

But again, how many PCs have more than 1 keyboard connected? For simple cases for PCs with just one keyboard - disconnection event is going to be a useful feature regardless!

Keyboards do not use a gamepad-style API for (dis)connecting, so this would require listening for USB event changes which is more difficult.

Is this any different from listening for disconnection events for controllers that are connected by USB? Isn't this practically the same thing? On the other hand I just checked the device manager and keyboards sections looks like this

image

Note: I only have a single keyboard connected! :) Razer thingy is the mouse.. I have no idea what it is doing in keyboard section..

If this topic/feature turns out to be too difficult to implement, just let me know and I will close this topic.. or just do it :)

@Calinou
Copy link
Member

Calinou commented Apr 15, 2023

Note: I only have a single keyboard connected! :) Razer thingy is the mouse.. I have no idea what it is doing in keyboard section..

That's the problem with USB – it's a wild west: godotengine/godot#20566

This doesn't inspire me confidence in implementing this feature without negative side-effects.

@KvaGram
Copy link

KvaGram commented Apr 20, 2023

I would like to add a flaw to the idea.
Most wireless mice and keyboards use a usb dongle that registers as the device. If the device runs out of power, the dongle won't just disconnect itself, will it? I don't think it works that way. So the OS would have no idea if a wireless mouse or keyboard lost power, and with that, it follows that Godot can't know either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants