- Simplified and refactored much of the code for the actual use cases, the granular/specific approach did not pan out and in some cases less ideal.

This commit is contained in:
Ryan Reaves
2019-07-29 21:29:26 -05:00
parent a22e05cebd
commit aa895a8be8
2 changed files with 83 additions and 130 deletions

View File

@@ -22,22 +22,18 @@ internalid = 0
usbid = 0
def keyboard_detect():
global internalid
global usbid
global internalid, usbid, chromeswap
internal_kbname = ""
usb_kbname = ""
print()
print("Looking for keyboards...")
print()
if laptop_kb == "1" or laptop_kb == "2":
result = subprocess.check_output('xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | grep -o -P "(?<=↳).*(?=id\=)";exit 0', shell=True).decode('utf-8')
if result != "":
internal_kbname = result.strip()
internalid = subprocess.check_output('xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | cut -d "=" -f 2- | awk \'{print $1}\';exit 0', shell=True).decode('utf-8')
print("Internal Keyboard\nName: " + internal_kbname + "\nID: " + internalid)
if laptop_kb == "1" or laptop_kb == "3":
result = subprocess.check_output('udevadm info -e | grep -o -P "(?<=by-id/usb-).*(?=-event-kbd)" | head -1;exit 0', shell=True).decode('utf-8')
if result != "":
usb_kbname = result.strip()
@@ -86,37 +82,36 @@ print()
input("Press Enter to begin...")
system_type = input("\nWhat type of system are you using?\n\
1) Windows\\Linux Laptop\n\
2) Chromebook Laptop\n\
3) Macbook\n\
4) Windows\Linux Desktop\n\
5) Chrome Desktop (w/ Chrome keyboard)\n\
6) Mac Desktop\n")
1) Windows\n\
2) Chromebook\n\
3) Mac\n")
swap_behavior = 1
# Chromebook
if system_type == "2" or system_type == "5":
if not input("\nWould you like to swap Alt to Super/Win and Search key to Ctrl when using terminal applications? (y/n)\n\
if system_type == "2":
if input("\nWould you like to swap Alt to Super/Win and Search key to Ctrl when using terminal applications? (y/n)\n\
Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y":
swap_behavior = 0
# Windows
if system_type == "1" or system_type == "4":
if not input("\nWould you like to swap Alt to Super/Win and Ctrl key back to Ctrl when using terminal applications? (y/n)\n\
if system_type == "1":
if input("\nWould you like to swap Alt to Super/Win and Ctrl key back to Ctrl when using terminal applications? (y/n)\n\
Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y":
swap_behavior = 0
# Mac
if system_type == "3" or system_type == "6":
if not input("\nWould you like to swap Command back to Super/Win and Ctrl key back to Ctrl when using terminal applications? (y/n)\n\
if system_type == "3":
if input("\nWould you like to swap Command back to Super/Win and Ctrl key back to Ctrl when using terminal applications? (y/n)\n\
Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y":
swap_behavior = 0
if int(system_type) < 4:
laptop_kb = input("\nWhat is your keyboard configuration?\n\
1) Both\n\
2) Built-in\n\
3) USB External\n")
else:
laptop_kb = 3
print(system_type + " " + str(swap_behavior))
if int(system_type) == 2 and swap_behavior == 0:
chromeswap = input("\nIf the keyswap is applied on a chromebook with both an internal and external Apple keyboard\n\
you may need to press a key on the external Apple keyboard any time you switch between the terminal and gui based apps.\n\
Are you ok with that, or would you like to only apply the keyswap on one keyboard type?\n\
1) Built-in\n\
2) Both - (Chromebook & Windows)\n\
3) Both - (Chromebook & Mac)\n\
4) USB External - (Mac)\n")
keyboard_detect()

View File

@@ -1,63 +1,37 @@
#!/bin/bash
class_name='konsole'
internalkb=$1
systemtype=$1
internalid=$2
usbkb=$3
usbid=$4
swapbehavior=$5
usbid=$3
swapbehavior=$4
# echo $1 $2 $3 $4
internalgui="setxkbmap -device $internalid -option "
internalterm="$internalgui"
usbgui="setxkbmap -device $usbid -option "
usbterm="$usbgui"
if [[ $internalkb == "windows" ]]; then
internalgui+="altwin:ctrl_alt_win"
internalterm+="altwin:swap_alt_win"
internalgrep_term="-q ctrl_alt_win"
internalgrep_gui="-v ctrl_alt_win\ 1>/dev/null"
elif [[ $internalkb == "chromebook" ]]; then
internalgui="xkbcomp -w0 -I$HOME/.xkb -i $internalid ~/.xkb/keymap/kbd.gui $DISPLAY"
internalterm+="altwin:swap_lalt_lwin"
# internalgrep_term="-vq swap_lalt_lwin"
internalgrep_term="-q swap_lalt_lwin"
internalgrep_gui="-v swap_lalt_lwin 1>/dev/null"
elif [[ $internalkb == "mac" ]]; then
internalgui+="ctrl:swap_lwin_lctl"
#mac term is blank
internalterm=""
internalgrep_term="-q swap_lwin_lctl"
internalgrep_gui="-v swap_lwin_lctl 1>/dev/null"
swapcmd_term="setxkbmap -option;setxkbmap -option altwin:swap_alt_win"
fallbackcmd_gui=""
if [[ "$systemtype" == "windows" || "$systemtype" == "mac" ]]; then
swapcmd_gui="setxkbmap -option;setxkbmap -option altwin:ctrl_alt_win"
check_gt="setxkbmap -query | grep -q 'ctrl_alt_win'"
check_tg="setxkbmap -query | grep -v 'ctrl_alt_win' 1>/dev/null"
elif [[ "$swapbehavior" == "both_mac" ]]; then
swapcmd_gui="setxkbmap -option;setxkbmap -option ctrl:swap_lwin_lctl; xkbcomp -w0 -i $internalid -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY"
swapcmd_term="setxkbmap -option;setxkbmap -device $internalid -option 'altwin:swap_alt_win'"
check_gt="setxkbmap -query | grep -v 'swap_alt_win' 1>/dev/null"
check_tg="setxkbmap -query | grep -q 'swap_alt_win'"
elif [[ "$swapbehavior" == "both_win" ]]; then
swapcmd_gui="setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY; setxkbmap -device $usbid -option altwin:ctrl_alt_win"
fallbackcmd_gui="setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY"
check_gt="setxkbmap -query | grep -q 'ctrl_alt_win'"
check_tg="setxkbmap -query | grep -v 'ctrl_alt_win' 1>/dev/null"
elif [[ "$swapbehavior" == "none" ]]; then
swapcmd_gui="setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY"
check_gt="setxkbmap -query | grep -v 'swap_alt_win' 1>/dev/null"
check_tg="setxkbmap -query | grep -q 'swap_alt_win'"
fi
if [[ $usbkb == "windows" ]]; then
usbgui+="altwin:ctrl_alt_win"
usbterm+="altwin:swap_alt_win"
usbgrep_term="-q ctrl_alt_win"
usbgrep_gui="-v ctrl_alt_win 1>/dev/null"
elif [[ $usbkb == "chromebook" ]]; then
usbgui="xkbcomp -w0 -I$HOME/.xkb -i $usbid ~/.xkb/keymap/kbd.gui $DISPLAY"
usbterm+="altwin:swap_lalt_lwin"
usbgrep_term="-q swap_lalt_lwin"
usbgrep_gui="-v swap_lalt_lwin 1>/dev/null"
elif [[ $usbkb == "mac" ]]; then
usbgui+="ctrl:swap_lwin_lctl"
#mac term is blank
usbterm=""
usbgrep_term="-q swap_lwin_lctl"
usbgrep_gui="-v swap_lwin_lctl 1>/dev/null"
fi
# echo "$internalgui"
# echo "$internalterm"
# echo "$usbgui"
# echo "$usbterm"
# exit
# echo "$systemtype $swapbehavior"
# echo "$swapcmd_gui"
# regex for extracting hex id's
grep_id='0[xX][a-zA-Z0-9]\{7\}'
@@ -74,49 +48,33 @@ while read -r id; do
if [ -n "$class" ]; then
# Set keymap for terminal, Alt is Super, Ctrl is Ctrl, Super is Alt
if [[ $internalid -gt 0 ]]; then
eval "$check_gt;echo $?"
if [ $? -eq 0 ]; then
echo "internal gui to term"
echo "$internalgrep_gui"
eval "setxkbmap -query | grep $internalgrep_gui"
if [ $? -eq 0 ]; then
echo "* inside internal gui to term"
echo "* $internalterm"
eval setxkbmap -device $internalid -option
eval $internalterm
eval "$swapcmd_term"
# Quick hack, will want to refactor later
# just resets required checks, for chromebooks that
# use usb windows keyboards
if [[ "$swapbehavior" == "both_win" ]]; then
check_gt="setxkbmap -query | grep -q 'ctrl_alt_win'"
check_tg="setxkbmap -query | grep -v 'ctrl_alt_win' 1>/dev/null"
fi
fi
if [[ $usbid -gt 0 ]]; then
echo "usb gui to term"
echo "$usbgrep_gui"
eval "setxkbmap -query | grep $usbgrep_gui"
if [ $? -eq 0 ]; then
echo "* inside usb gui to term"
echo "* $usbterm"
eval setxkbmap -device $usbid -option
eval $usbterm
fi
fi
else
# Set keymap for gui, Alt is Ctrl,Super is Alt, Ctrl is Super
if [[ $internalid -gt 0 ]]; then
eval "$check_tg;echo $?"
if [ $? -eq 0 ]; then
echo "internal term to gui"
echo "$internalgrep_term"
eval "setxkbmap -query | grep $internalgrep_term"
if [ $? -eq 0 ]; then
echo "** inside internal term to gui"
echo "** $internalgui"
eval "setxkbmap -device $internalid -option"
eval "$internalgui"
eval "$swapcmd_gui;echo $?"
if [ $? -eq 0 ] && [[ "$swapbehavior" == "both_win" ]]; then
eval "$fallbackcmd_gui"
check_gt="setxkbmap -query | grep -v 'swap_alt_win' 1>/dev/null"
check_tg="setxkbmap -query | grep -q 'swap_alt_win'"
fi
fi
if [[ $usbid -gt 0 ]]; then
echo "usb term to gui"
echo "$usbgrep_term"
eval "setxkbmap -query | grep $usbgrep_term"
if [ $? -eq 0 ]; then
echo "** inside usb term to gui"
eval setxkbmap -device $usbid -option
eval $usbgui
fi
fi
fi
timestp=$(date +%s)