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
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, 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.
That's a nice workaround @pbodnar.
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!
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
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
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...
@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.
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.
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?
Absolutely! Nice one 馃槃
Most helpful comment
Changing it to following:
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?