How can l map a command to prtsc-key ?

I'm using ImageMagick to get screenshot ,but this import command can be only used on terminal .So I want to map the import command to my prtsc-key.
In Fvwm WM (V2 or 3)

Code:
Key Print A A Exec exec [your script]
In the .fvwm2rc o config file

Its depends of what WM you use
 
Presuming you use X11, have a look at x11/xbindkeys. First, determine the name of the key you want to map with xev(1). Start the program and press the key. The output contains:​
Code:
[…] (keysym 0xff61, Print) […]
Mind Print and kill the program with Control‑C. Secondly, configure xbindkeys(1). For example, my ~/.xbindkeysrc contains:​
Bash:
"ratpoison -c 'exec mlterm -e orpie'"
XF86Calculator
When the calculator button is pressed, the preceding command is executed.

Finally, ensure xbindkeys(1) is started when you’re starting your graphical session. For example my ~/.xinitrc looks like this​
Bash:
xbindkeys   # starts by default as a daemon (i. e. in background)
ratpoison   # this is a decoration‑less window manager you can control via keyboard only
unclutter   # this program hides the mouse pointer if it hasn’t moved for some time
If you employ a different mechanism for starting your graphical session, you’ll need to use the proper “auto start” feature.​
 
I'm using ImageMagick to get screenshot ,but this import command can be only used on terminal .So I want to map the import command to my prtsc-key.
I use x11/xbindkeys for that.

I use [Print Screen] to do screenshot of entire screen and [Shift]+[Print Screen] to do the same with a delay.
Code:
% cat ~/.xbindkeysrc | grep -C 2 Print

"~/scripts/shot.sh"
  Print

"~/scripts/shot-delay.sh"
  Shift + Print

You just need to have xbindkeysrc running - it will pick up the ~/.xbindkeysrc automatically.

Hope that helps.
 
Many window managers and desktop environments allow you to configure your own keyboard shortcuts. What x11/xbindkeys is offering is build-in in most of those WM's and DE's. This doesn't mean xbindkey is useless, on the contrary, it has it's use cases.

Here are some examples of custom keyboard shortcuts in WM's I'm using on my system to take screenshots with graphics/maim from a shell script called "maimss", located in the users PATH.

The key combination is windows key (superkey, mod4) and PrtSc (Print Screen).

x11-wm/openbox:

~/.config/openbox/rc.xml
Code:
    <keybind key="W-Print">
      <action name="Execute">
        <command>maimss</command>
      </action>
    </keybind>

x11-wm/jwm

~/.jwmrc
Code:
    <Key mask="mod4" key="Print">exec:maimss</Key>

x11-wm/icewm

~/.icewm/keys
Code:
key "Super+Print"        maimss

x11-wm/herbstluftwm

~/.config/herbstluftwm/autostart
Code:
hc keybind Mod4-Print spawn maimss

As you can see, some of the WM's bind key syntax is a short one liner.

I do not use following DE's, just to add to the list.

XFCE4, Gnome, KDE
 
Many window managers and desktop environments allow you to configure your own keyboard shortcuts. What x11/xbindkeys is offering is build-in in most of those WM's and DE's.
I use Openbox - and Openbox allows to configure 'some' shortcuts for executing anything - but its very limited.

For example - its not possible to assign something to JUST [Print Screen] button. There ALWAYS needs to be a modifier in front of it - so [Shift]+[Print Screen] will work or [CTRL]+[Print Screen] will work - but not [Print Screen] alone.

This is why after searching for a while - I moved to xbindkeys for some shortcuts.
 
In motif (mwm) you can put the following in your .mwmrc. Or if you use emwm, your .emwmrc.

Code:
Keys DefaultKeyBindings
{
...
<Key>Sys_Req            root|window|icon        f.exec "/usr/local/bin/xterm -ls"
...
}

In CDE you can put the following in your .dt/dtwmrc.

Code:
Keys DtKeyBindings
{
...
<Key>Sys_Req            root|window|icon        f.exec "/usr/local/bin/xterm -ls"
...
}

Of course replace "/usr/local/bin/xterm -ls" with "application command line of your choice".
 
Related but maybe derailing:
Windowmaker recently added hooks for the PrintScreen key to capture the screen; this binding was done at the windowmanager level. For some reason on my system and a few others it wound up capturing the Up Arrow key, which made everything difficult :)

So should binding keys to actions beyond simple window actions (open close raise lower maximize minimize) be done by the windowmanager or should a utility be used to bind keys?

My opinion, I think windowmanagers/DEs should do as little as possible. Having a widget to make it easy for a user to do the binding is a good thing.
 
its not possible to assign something to JUST [Print Screen] button. There ALWAYS needs to be a modifier in front of it - so [Shift]+[Print Screen] will work or [CTRL]+[Print Screen] will work - but not [Print Screen] alone.
Works for me, just tested.

I have bound the execution of the screenshot script to the [Print Screen] (PrtSc) key only. The script was executed, a screenshot of the desktop was taken.

~/.config/openbox/rc.xml
Code:
    <keybind key="Print">
      <action name="Execute">
        <command>maimd</command>
      </action>
    </keybind>

("maimd" is actually "capture the whole desktop" script, "maimss" from example in posting #6 "select screen area or window and capture").

The Openbox version on my system is 3.6_10. Could it be that an earlier version of Openbox was not able to bind to a single key, and you remained with this assumption with newer versions?


Openbox allows to configure 'some' shortcuts for executing anything - but its very limited.
That sound right. I couldn't convince openbox to accept to execute a command substitution in a command string, I had to put it in a script.

<command>maim ~/Pictures/Screenshots/Screenshot-`date +%F+%T`.png</command> resulted always in Screenshot-`date. What I expected was Screenshot-2024-04-13-09:50:54.png
 
Related but maybe derailing:
Windowmaker recently added hooks for the PrintScreen key to capture the screen; this binding was done at the windowmanager level. For some reason on my system and a few others it wound up capturing the Up Arrow key, which made everything difficult :)

So should binding keys to actions beyond simple window actions (open close raise lower maximize minimize) be done by the windowmanager or should a utility be used to bind keys?

My opinion, I think windowmanagers/DEs should do as little as possible. Having a widget to make it easy for a user to do the binding is a good thing.
For traditional X environments like mwm, CDE, fvwm, and others, one could map the key to execute a shell script that would invoke xwd to capture the screen, writing the output to an arbitrary file of the person's choosing. Then use xwud to either display the output or use some tool to convert it to jpeg, which in turn can be attached to an email.

I rarely attach screenshots to my FreeBSD email but when I do I use xwd. At $JOB we use Outlook. Everyone, including me, attaches junk to their emails.

Anyone remember when emails (email files) were kilobytes in size? Now most everything is megabytes. It's easy to have an SMTP daemon reject emails due to size limitations.
 
Back
Top