Skycoder42 / QHotkey

A global shortcut/hotkey for Desktop Qt-Applications
BSD 3-Clause "New" or "Revised" License
552 stars 162 forks source link

BadAccess when try to register Meida keys in linux #6

Closed mohsenti closed 6 years ago

mohsenti commented 7 years ago

Hi,

I develop a simple audio player with Qt, now I have handle mediakeys like play/pause , next , previous and others but when try to register hotkeys a badaccess exception occur in code. Is it a library bug ?

error message : QHotkey: Failed to register hotkey. Error: BadAccess (attempt to access private resource denied)

Skycoder42 commented 7 years ago

I'm afraid it's not a bug, but a limitation. As stated in the README, not all keys will work for all platforms. And as it seems, this is true for media keys on linux.

This does not mean, it's impossible, but you can't use the Qt keycodes like Qt::Key_Play. This is because the algorithm that mapps thouse keys is not complete, as I wasn't able to find a solution that works for all keys.

As a solution, try find out the native keycode on linux, and try this one

QHotkey::NativeShortcut native;
native.key = /*native keycode for "play" here*/;
auto hKey = new QHotkey(native);

If this still triggers an error, it is impossible to register the keys.

ArsenArsen commented 6 years ago

Other things utilizing these keys use GNOME's daemon. You could look at how GNOME handles it. By the way here are the X keysyms for media buttons:

XF86AudioPlay
XF86AudioPrev
XF86AudioNext
XF86AudioStop
XF86HomePage
Skycoder42 commented 6 years ago

Closing, as it seems no further help is needed

rodlie commented 5 years ago

So, any solutions for this?

    // XF86MonBrightnessUp 0x1008FF02 233
    QHotkey::NativeShortcut native;
    native.key = 233;
    qDebug() << "KEY?" << native.isValid();
    auto hotkey = new QHotkey(native, true, this);
    qDebug() << "OK?" << hotkey->isRegistered();

I get false on both.

Skycoder42 commented 5 years ago

You have to create a native shortcut as

QHotkey::NativeShortcut native (233);

Thats an artifact of NativeShortcut originally beeing a private class, thus the validity does not get updates when settings the key/modifier directly... I shall fix that in the next release.

After creating the native shortcut that way, at least the first line should return true. For the second one, there is once again no guarantee. If it works, it works. If not, it's not possible with the "normal" keygrabber API that is used by this library.

rodlie commented 5 years ago

Thanks, that did the trick.

I do however need a way to get the keycode automatically from XF86MonBrightnessUp somehow (will do some reading, suggestions welcome).