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

In Menu shortcut override Global shortcut #1435

Closed
beefeater7 opened this issue Jul 11, 2020 · 10 comments
Closed

In Menu shortcut override Global shortcut #1435

beefeater7 opened this issue Jul 11, 2020 · 10 comments
Labels
feature New feature request

Comments

@beefeater7
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I'm working on a 'global' hotkey that adapts to its environment. If CopyQ focused, do X, if not, do Y. This is how far I got.

[Command]
Name=Contextual Shortcut
Command="
    copyq:
    popup(str(input()) || str(clipboard()))"
Input=text/plain
InMenu=true
IsGlobalShortcut=true
Icon=\xf15b
Shortcut=shift+f3, shift+f4
GlobalShortcut=shift+f4
  • Try S-F3 in app, S-F4 outside app: Works.
  • Try S-F4 in app. input() falls through to clipboard() because Global overrides App.

Describe the solution you'd like
S-F4 works like S-F3 when app is focused.

Describe alternatives you've considered
I can always use S-F3 and S-F4 depending on context, but this seems neater.

Another option is to let Global shortcuts access app functions like input, if application is focused.

@beefeater7 beefeater7 added the feature New feature request label Jul 11, 2020
@pbodnar
Copy link
Contributor

pbodnar commented Jul 11, 2020

@beefeater7, this is an interesting problem. In the Convert Markdown to ... command, I fallback to copy() + clipboard() if not getting anything from input(), so the command works from the main window (i. e. "application") as well, even though I define just a global shortcut for the command. But yes, it would be nice to find an easier way of how to handle those situations, maybe like you suggest:

Another option is to let Global shortcuts access app functions like input, if application is focused.

@beefeater7
Copy link
Contributor Author

That's a nice workaround @pbodnar.

@beefeater7
Copy link
Contributor Author

I actually thought I had a magic solution here - the temporary kind at least...

[Command]
Name=Workaround
Command="
    copyq:
    var winTitle = currentWindowTitle().match(/- (.*?)$/)
    var program = winTitle[winTitle.length-1]
    
    if (program == 'CopyQ') {
        popup(program)
        copy()
    }
    
    popup(str(clipboard()))"
Input=text/plain
IsGlobalShortcut=true
Icon=\xf15b
GlobalShortcut=shift+f4

Using the copy()+clipboard() idea as base, I fleshed it out with some checks. This doesn't actually work though. 80% of the time, this triggers an error when used in the application. (the regex works fine though, feel free to use it)

ScriptError: Exception in command "Workaround": Failed to copy to clipboard!

@beefeater7
Copy link
Contributor Author

beefeater7 commented Jul 11, 2020

Ok, this is getting weird. It's safe to say we need a proper fix at this point. @pbodnar, you sure that trick is working for you?

[Command]
Name=input() substitute
Command="
    copyq:
    copy()
    popup(clipboard())
    
    //<EMPTY> leads to ScriptError
    //Copy in application once doesn't work,
    //but spamming the command 
    //allows items to pass through"
IsGlobalShortcut=true
Icon=\xf15b
GlobalShortcut=shift+f4

@hluk
Copy link
Owner

hluk commented Jul 12, 2020

Let me share a few hints. I'm not exactly sure what you need to do, but I hope this helps.

You can use focused() to check if the CopyQ main window has focus.

You can also check, if needed, the shortcut that triggered the command:

var shortcut = str(data(mimeShortcut))
if (shortcut == "shift+f4")
    ...

The input() returns a valid value only when the command is triggered from main window (not by a global shortcut) or ran from command line.

The copy() without any arguments basically sends Ctrl+C to current window, it raises the exception (Failed to copy) in case it doesn't detect clipboard change in an interval.

See also: https://copyq.readthedocs.io/en/latest/scripting-api.html

@pbodnar
Copy link
Contributor

pbodnar commented Jul 12, 2020

Using the copy()+clipboard() idea as base, I fleshed it out with some checks. This doesn't actually work though. 80% of the time, this triggers an error when used in the application. (the regex works fine though, feel free to use it)

ScriptError: Exception in command "Workaround": Failed to copy to clipboard!
...
Ok, this is getting weird. It's safe to say we need a proper fix at this point. @pbodnar, you sure that trick is working for you?

In fact for me (on Windows 10), this seems to depend on the shortcut used. When using for example shift+f4, copy() always succeeds. But when using meta+alt+c, it looks like Windows or something steals focus from the actual window if the c key is not released fast enough, co copy() fails possibly because of that. Maybe file a new issue for that? I was too lazy to do that previously. ;)

@hluk, thanks for your useful tips, mainly the focused() function can come handy, it seems...

@beefeater7
Copy link
Contributor Author

@hluk Huh, I completely overlooked the mimetype variables, thanks! However focused() doesn't help here.

[Command]
Name=Check Focus
Command="
    copyq:
    if (focused()) {
        copy()
    }
    popup(clipboard())
"
IsGlobalShortcut=true
Icon=\xf15b
GlobalShortcut=ctrl+\xa7, shift+f4

@pbodnar This issue actually lies at the core of this. It was resolved in a past commit, but I hopped on a later build without the fix.

I tried pressing the hotkeys super quick and the script works. With v3.12.0 in the pipeline it's a done deal.

However, my feature request stands. It would be useful for app menu shortcuts to override global.

@beefeater7 beefeater7 changed the title Application > Global shortcut In Menu > Global shortcut Jul 12, 2020
@beefeater7 beefeater7 changed the title In Menu > Global shortcut In Menu shortcut override Global shortcut Jul 12, 2020
@hluk
Copy link
Owner

hluk commented Jul 12, 2020

I tried pressing the hotkeys super quick and the script works. With v3.12.0 in the pipeline it's a done deal.

Glad it works for you. Version 3.12.0 is fresh out of the press! :)

However, my feature request stands. It would be useful for app menu shortcuts to override global.

I'll take a look into this if I have time. But currently I don't see this as being a big problem since I don't see benefit in using both application and global shortcut in the commands I used or have created in the past.

hluk added a commit that referenced this issue Jul 14, 2020
If global shortcut for a menu command is triggered when the main window
is active, the command will be executed as if it has been trigger from
menu - i.e. with item selection and item data available.

Fixes #1435

Signed-off-by: Lukas Holecek <hluk@email.cz>
hluk added a commit that referenced this issue Jul 14, 2020
If global shortcut for a menu command is triggered when the main window
is active, the command will be executed as if it has been trigger from
menu - i.e. with item selection and item data available.

Fixes #1435

Signed-off-by: Lukas Holecek <hluk@email.cz>
@hluk
Copy link
Owner

hluk commented Jul 14, 2020

Changing it to following:

If global shortcut for a menu command is triggered when the main window
is active, the command will be executed as if it has been triggered from
menu - i.e. with item selection and item data available.

Basically, if you check "In Menu" and "Global Shortcut" in a command and press the global shortcut when the main window has focus, then input(), currentItem() and similar functions will work.

Does it make sense?

@beefeater7
Copy link
Contributor Author

Absolutely! Nice one 😄

hluk added a commit that referenced this issue Jul 15, 2020
If global shortcut for a menu command is triggered when the main window
is active, the command will be executed as if it has been triggered from
menu - i.e. with item selection and item data available.

Fixes #1435

Signed-off-by: Lukas Holecek <hluk@email.cz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature request
Projects
None yet
Development

No branches or pull requests

3 participants