1. Registering/capturing shortcut keys in Linux?
- Posted by TheDcoder Nov 29, 2017
- 1259 views
Hello, I recently found out about Euphoria and loved what I read :)
I am getting started today with a small program or script which does actions when the user presses a shortcut/hotkey. I am having trouble figuring out how I would register shortcuts on linux, on Windows I could probably use the provided Win32 API but not an option on Linux
Preferably I want an example of a simple script which uses hotkeys/shortcuts and is not platform specific... but I am fine if it only works in Linux.
Thanks for the help in advance!
2. Re: Registering/capturing shortcut keys in Linux?
- Posted by Senator Nov 29, 2017
- 1247 views
Preferably I want an example of a simple script which uses hotkeys/shortcuts and is not platform specific... but I am fine if it only works in Linux.
I am not so sure about simple examples, but if you use Pete Eberlein's euphoria source code editor, WEE, you can find a number of examples of how he implemented hotkeys using Irv Mullin's EuGTK. If you do not have WEE, you can down load it very easily: Create a folder called "Wee" and download updater.ex. Run "eui updater.ex" to download/update the files for your platform. Run "eui wee" to run the editor, or compile/bind/shroud it for convenience.
For a non-GUI example of how hotkeys are implemented under Linux you can study the sample editor edx.ex which is found in the euphoria installations bin directory. The relevant code can be found at the very beginning of the edx.ex source code.
Regards, Ken Rhodes
3. Re: Registering/capturing shortcut keys in Linux?
- Posted by TheDcoder Nov 29, 2017
- 1230 views
I already have Wee installed. I do not want to use GTK just for shortcut keys and my simple project isn't GUI based anyway. I think it would be a bit hard to directly refer to the source code... ne1uno has pointed me towards the demo directory, I will check the examples to see if they contain any which demonstrate the shortcut key functionality.
I will also post my findings here :)
4. Re: Registering/capturing shortcut keys in Linux?
- Posted by irv Nov 29, 2017
- 1247 views
Welcome!
There are no "signals" when using text-mode programs, so you'll need to set up your own loop which checks for keypresses, and handles those you are interested in. The following will show how to detect the common ctl-key combinations such as are used in vi, nano, etc.
atom key while 1 do key = get_key() if key != -1 then if key > 0 and key < 27 then printf(1," ctl-%s %d",{key+64,key}) -- for demo purposes; end if switch key do case 5 then -- do stuff with ctl-e; case 10 then puts(1,key) -- this is the enter key, might as well use it; case 23 then -- do stuff with ctl-w; case 127 then puts(1,{8,32,8}) -- destructive backspace; -- etc case else if key > 26 then puts(1,key) end if -- send to screen; end switch end if end while -- ctl-a thru ctl-z return 1..26 (case insensitive)
Beware! this will not change unix keys that already are assigned, such as ctl-z, which puts your program into the background, or ctl-c, which will end your program execution, nor will this simple routine handle alt-keys, which are actually esc- sequences. More complex code is required to handle those.
If you type stty -a in a terminal, you'll see a list of the ctl-keys that are already used.
5. Re: Registering/capturing shortcut keys in Linux?
- Posted by TheDcoder Nov 29, 2017
- 1145 views
I think you guys are misunderstanding what I want to do. I do not want to capture keys from the console/terminal. I want to hook to keyboard shortcuts like Ctrl+Alt+1, Alt+P, Ctrl+G etc.. My program may or may not be run from a terminal, it can also be called by another program.
So the basic idea is that it runs in the background without any interface only listening for shortcut keys assigned to it. Something similar to XBindKeys but it is only capable of mapping commands to keys. I want to directly map my code to the shortcut keys so that I can perform some actions when a shortcut key is pressed.
6. Re: Registering/capturing shortcut keys in Linux?
- Posted by irv Nov 30, 2017
- 1177 views
Perhaps Autokey will give you an idea. It appears to use a daemon running in the background to trap keys. Examine the sourcecode to see how it works.
An Autokey script to launch eu and a program looks like this:
output = system.exec_command("eui ~/demos/BEAR") keyboard.send_keys(output)
You could send parameters along with the command if needed.
However, affecting a running program is going to be a bigger deal. You'll probably have to communicate via files or pipes.
7. Re: Registering/capturing shortcut keys in Linux?
- Posted by TheDcoder Nov 30, 2017
- 1152 views
AutoKey? Never heard about it... but it sounds like it is similar to XbindKeys... I can't really "look" at the source code to figure out how it works. My programming experience is limited to BASIC languages like AutoIt, I don't really understand the terminology used in the other codes
Looks like I have to settle with XbindKeys and register some commands that communicate with the main process. I will have to look into inter-process communication, any tips for a newbie?
8. Re: Registering/capturing shortcut keys in Linux?
- Posted by irv Nov 30, 2017
- 1147 views
Autokey is about 50 lines of python. However, it communicates via dbusSession calls, which I know nothing about.
For communicating between programs, look up "pipe" in the Archives.
9. Re: Registering/capturing shortcut keys in Linux?
- Posted by TheDcoder Nov 30, 2017
- 1142 views
Autokey is about 50 lines of python. However, it communicates via dbusSession calls, which I know nothing about.
For communicating between programs, look up "pipe" in the Archives.
dbusSessions looks like it is complicated... not really my thing. I have had a look at pipes.ex and it does a pretty good job of demonstrating IPC using pipes, but there is one small issue... It works by executing a command/program from the code, that is not I need. I will have to communicate with an already running program.
The basic idea is to make a script which uses XBindKeys to bind the shortcut keys to the script with different commandline switches and when the shortcut keys execute the script the the commandline switches, they communicate with the main process and inform it that a shortcut key had been pressed.
I might end up using networking/sockets to do IPC or even worse, UNIX signals which is not cross-platform :(