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

[Feature] Expose a way to add custom global keybinds from within a plugin #1609

Open
1 task done
NatanielRegula opened this issue Apr 12, 2023 · 4 comments
Open
1 task done

Comments

@NatanielRegula
Copy link

Before Requesting

  • I found no existing issue matching my feature request

Describe the feature you'd like!

It would be nice if you could add the ability for a plugin to add/register custom keybinds that work globally (even if discord is not in focus), whether via exposing globalShortcut in require("electron") or by adding a wrapper of some kind that allows for adding custom global keybinds.

Anything else?

https://www.electronjs.org/docs/latest/tutorial/keyboard-shortcuts#global-shortcuts

@Vendicated
Copy link

Vendicated commented Jun 20, 2023

you can use Discord's api for this:

DiscordNative.nativeModules.requireModule("discord_utils").inputEventRegister(
    1337,
    [
         [0, 0xA2], // LEFT CTRL
         [0, 0x42], // B
    ],
    isDown => console.log(isDown),
    {
        blurred: true,
        focused: true,
        keydown: true,
        keyup: true
    }
)
  • the first argument is a numeric id that you can later use to unregister the handler again
  • the second argument is an array of keycodes. I'm not sure what the first number means, you can just pass 0 for it. The second number is the virtual keycode for the specific key. These differ per platform but Discord has keycode mappings for each platform. You can find all 3 of these mappings via getModuleByProps("ctrl") In my example, i am on Windows and use 0xA2 (left control) + 0x42 (B)
  • the third argument is either the callback or the literal string "UNASSIGNED". It will be fired whenever the key is pressed or released, and gives you the boolean state as argument
  • the final argument is an options object:
    • blurred: keybind active when discord not focused
    • focused: keybind active when discord focused
    • keydown: fire callback for key down (isDown = true)
    • keyup: fire callback for key up (isDown = false)

Obtain keycodes for each platform (you might or might not have to add searchExports: true):

const WindowsMappings = getModule(m => m.ctrl === 0xa2)
const LinuxMappings = getModule(m => m.ctrl === 0x25)
const MacMappings = getModule(m => m.ctrl === 0xe0)

To unregister:

DiscordNative.nativeModules.requireModule("discord_utils").inputEventUnregister(1337)

@NatanielRegula
Copy link
Author

Thanks! I can confirm this works. Though it isn't an official bd api I'll close this issue for now. If anyone comes up with a better solution in the future they can re-open.

@rauenzi
Copy link
Member

rauenzi commented Jun 21, 2023

Definitely not a good reason to close imo as the original issue/feature here is not addressed

@NatanielRegula
Copy link
Author

Makes sense, I was under the impression that this was not a huge concern for BD. But I'm happy that it's still being considered an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📋 Backlog
Development

No branches or pull requests

3 participants