The i3 FAQ has migrated to https://github.com/i3/i3/discussions. All content here is read-only.
Ask Your Question
1

Execute command when Mod is pressed?

asked 2014-05-15 02:22:48 +0000

SuperZanti gravatar image

updated 2014-09-11 23:48:51 +0000

Every time I press the mod button I'd like to execute a command to update my i3bar. Is this possible?

Solution:

I found the solution. In my i3 config I put:

bindcode 133 exec my_command

Where '133' is the keycode of my Mod key, then I can run my_command each time it's pressed. So I can update my i3blocks bar by putting:

bindcode 133 exec pkill -SIGRTMIN+1 i3blocks

in the config, then all the blocks with a signal of 1 will be updated.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2014-05-15 09:35:12 +0000

phairland gravatar image

updated 2014-05-15 09:41:38 +0000

Mod keys like alt or super are meant to be use in combination with others keys. When you are trying to assign a shortcut either on the i3 config or others programs like xbindkeys and xdotool, it is always expecting one of the modifiers keys plus the combination key. I do not think you will be able to map a command as soon as you press a modifier key, but maybe there is a way to do it.

The other issue that you seem to present is that you want to update the i3bar, and the right way to do it is through the i3status.conf. You can actually use any programming language or a program that outputs data. My i3 bar consists of weather, bitcoin exchange rate, cpu load, temp, battery, dropbox status, wifi status, volume, date and time. All these I get by using tcl to call commands and format the output with sed, awk, grep and cut.

The file output a JSON format that i3bar can understand, so basically you can use any programming language or program that outputs data and format it according to the i3 JSON guidelines.

Besides my program outputting all that data onto the i3bar, it will change color according to the status. For example, temp is green when is less than 65, yellow when is between 65 and 74 and red when is more than 74. Wifi and Dropbox are red when offline, and cpu load follows the same pattern. Another is the charge when laptop on batteries, it will turn red when less tha 20%.

Then you can add almost anything including things you can find on the web, like weather and exchange rates. Most programming languages have http apis that provides you with tools to extract information from webpages, and that's why the i3 bar is so powerful.

Tcl code is quite straight forward and you can look at the code and modify it in your programming language of choice and since most of the formatting is done using external programs like sed and awk. The only thing you need to be wary of is how tcl handles awk code, for example when in tcl code awk {{print $3}} in other programming languages and awk itself is awk '{print $3}' as there is special case how tcl handles single quotes, so it have to use curly braces instead.

On the i3.conf I got the following line to call my tcl file:

bar {
position top
status_command i3status --config ~/.config/i3/i3status.conf | ~/.config/i3/status.tcl
colors {
urgent_workspace   #000000 #000000 #c0c0c0
}
}

Then my status.tcl file:

#!/bin/tclsh
package require http
::http::config -useragent "Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0"
set    red "#FF0000"
set  green "#00FF00"
set yellow "#FFF000"
set   blue "#00EAFF" 
set  white "#FFFFFF" 
puts {{"version":1}
[
[]}
proc dropbox {} {
    catch { exec dropbox status} msg 
    set dropbox $msg
    if {$dropbox=="Dropbox isn't running!" || [string match Connecting* $dropbox]} {
        set stdout {{"name ...
(more)
edit flag offensive delete link more

Comments

1

Thanks for the lengthy comment! It's much appreciated! However, I am looking for something so I can only update the i3bar when the mod button is pressed. I know something has to be triggered because I have my bar hidden, and something has to run to show it again.

SuperZanti gravatar imageSuperZanti ( 2014-05-15 09:50:24 +0000 )edit
1

answered 2014-05-15 10:27:13 +0000

SuperZanti gravatar image

I found the solution. If I run

bindcode 133 exec my_command

Where '133' is the keycode of my Mod key, then I can run my_command each time it's pressed.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-05-15 02:22:48 +0000

Seen: 426 times

Last updated: Sep 11 '14