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

Add a keyboard switch script #356

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions polybar-scripts/system-keyboard-switch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Script: system-keyboard-switch

A shell script that shows the current keyboard layout and change it to another one

![switch](screenshots/switch_layout.png)

## Module

```ini
[module/system-keyboard-switch]
type = custom/script
exec = ~/polybar-scripts/system-keyboard-switch.sh
tail = true
format-prefix = ""
format-prefix-foreground = #00ff00
format-underline = #ff0000
click-left = "kill -USR1 $(pgrep --oldest --parent %pid%)"
interval = 3
```
## Customization

By default there is only `fr` or `us` layout.
If you need to switch between other ones just change the script with your needed layouts.

## Moreover

I made a special module with script that do the trick.
Alongside with the module `xkeyboard` it looks great in that order of module: `[ ... system-keyboard-menu xkeyboard ... ]`

![menu-closed](screenshots/menu_layout_closed.png)
![menu-opened](screenshots/menu_layout_opened.png)

```ini
[module/xkeyboard]
type = internal/xkeyboard
blacklist-0 = num lock

format-prefix = ""

label-layout = %layout%

label-indicator-padding = 2
label-indicator-margin = 1

[module/system-keyboard-menu]
type = custom/menu

expand-right = true

format-spacing = 1

label-open = " #1 "
label-close = "#2 cancel"
label-separator = |

menu-0-0 = us
menu-0-0-exec = setxkbmap us -model pc105
menu-0-1 = fr
menu-0-1-exec = setxkbmap fr -model pc105

menu-2-0 = cancel
menu-2-0-exec = #mykeyboard.open.0

```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions polybar-scripts/system-keyboard-switch/system-keyboard-switch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

sleep_pid=0

layout=$(setxkbmap -query | grep layout | awk '{print $2}')

toggle() {
layout=$(setxkbmap -query | grep layout | awk '{print $2}')
if [ "$layout" = "fr" ]; then
layout="us"
elif [ "$layout" = "us" ]; then
layout="fr"
fi
if [ "$sleep_pid" -ne 0 ]; then
kill $sleep_pid >/dev/null 2>&1
fi
}

trap "toggle" USR1

while true; do
setxkbmap "$layout" -model pc105
echo " $layout"
sleep 1 &
sleep_pid=$!
wait
done