diff --git a/KeyList.json b/KeyList.json index 0d3f271..263a733 100644 --- a/KeyList.json +++ b/KeyList.json @@ -1,3 +1,3 @@ [ - ["ctrl", "alt", "j", "ls"] + ["ctrl", "alt", "j", "'{programPath}/key/sendkeys.sh' a QQ 4"] ] \ No newline at end of file diff --git a/key-get.py b/key-get.py index f2f956f..4ef264e 100644 --- a/key-get.py +++ b/key-get.py @@ -10,13 +10,13 @@ keyList = [] '''keyMap = [ [keyboard.Key.ctrl, keyboard.Key.alt, "j", "ls"] ]''' +programPath = os.path.split(os.path.realpath(__file__))[0] # 返回 string keyChangeMap = [ ["ctrl", keyboard.Key.ctrl], ["alt", keyboard.Key.alt], ["esc", keyboard.Key.esc], - ["enter", keyboard.Key.enter], + ["enter", keyboard.Key.enter] ] -programPath = os.path.split(os.path.realpath(__file__))[0] # 返回 string file = open(f"{programPath}/KeyList.json", "r") keyMap = json.loads(file.read()) for i in range(len(keyMap)): @@ -24,6 +24,12 @@ for i in range(len(keyMap)): for j in keyChangeMap: if keyMap[i][k] == j[0]: keyMap[i][k] = j[1] + continue + try: + keyMap[i][k] = keyMap[i][k].replace("{programPath}", programPath) + except: + pass + def on_press(key): try: @@ -48,7 +54,6 @@ def on_release(key): def ReadKey(): next = False - command = "" for i in keyMap: for k in range(0, len(i) - 1): k = i[k] @@ -69,11 +74,11 @@ def Read(): time.sleep(0.01) # Lock 锁防止多次调用 -if os.path.exists("/tmp/deepin-wine-runner-keyboard-lock"): +'''if os.path.exists("/tmp/deepin-wine-runner-keyboard-lock"): print("不可多次调用") print("锁 /tmp/deepin-wine-runner-keyboard-lock 已存在") sys.exit(1) -os.mknod("/tmp/deepin-wine-runner-keyboard-lock") +os.mknod("/tmp/deepin-wine-runner-keyboard-lock")''' threading.Thread(target=Read).start() # Collect events until released with keyboard.Listener( diff --git a/key/sendkeys.exe b/key/sendkeys.exe new file mode 100755 index 0000000..cf6d9de Binary files /dev/null and b/key/sendkeys.exe differ diff --git a/key/sendkeys.sh b/key/sendkeys.sh new file mode 100755 index 0000000..e45511c --- /dev/null +++ b/key/sendkeys.sh @@ -0,0 +1,107 @@ +#!/bin/bash + +# $1 key value +# $2 process name , default QQ|TIM +# $3 control mode , default ctrl+alt +# 0 no control key +# 1 shift+ +# 2 ctrl+ +# 3 alt+ +# 4 ctrl+alt+ +# 5 ctrl+shift+ +# 6 alt+shift+ + +SHELL_DIR=$(dirname $0) +SHELL_DIR=$(realpath "$SHELL_DIR") +if [ $SPECIFY_SHELL_DIR ]; then + SHELL_DIR=$SPECIFY_SHELL_DIR +fi + +get_wine_by_pid() +{ + cat /proc/$1/maps | grep -E "\/wine$|\/wine64$|\/wine |\/wine64 " | head -1 | awk '{print $6}' +} + +is_wine_process() +{ + wine_module=$(get_wine_by_pid $1) + if [ -z "$wine_module" ];then + wine_module=$(cat /proc/$1/maps | grep -E "\/wineserver$" | head -1) + fi + echo $wine_module +} + +get_prefix_by_pid() +{ + WINE_PREFIX=$(xargs -0 printf '%s\n' < /proc/$1/environ | grep WINEPREFIX) + WINE_PREFIX=${WINE_PREFIX##*=} + if [ -z "$WINE_PREFIX" ] && [ -n "$(is_wine_process $1)" ]; then + #不指定容器的情况用默认容器目录 + WINE_PREFIX="$HOME/.wine" + fi + WINE_PREFIX=$(realpath $WINE_PREFIX) + echo $WINE_PREFIX +} + +get_bottle_path_by_process_id() +{ + PID_LIST="$1" + PREFIX_LIST="" + + for pid_var in $PID_LIST ; do + WINE_PREFIX=$(get_prefix_by_pid $pid_var) + for path in $(echo -e $PREFIX_LIST) ; do + prefix=${path#*=} + if [ "$prefix" == "$WINE_PREFIX" ]; then + WINE_PREFIX="" + fi + done + if [ -n "$WINE_PREFIX" ];then + PREFIX_LIST+="\n$pid_var=$WINE_PREFIX" + fi + done + echo -e $PREFIX_LIST +} + +get_bottle_path_by_process_name() +{ + PID_LIST="" + for pid_var in $(ps -ef | grep -E -i "$1" | grep -v grep | awk '{print $2}');do + #通过判断是否加载wine来判断是不是wine进程 + if [ -n "$(is_wine_process $pid_var)" ];then + PID_LIST+=" $pid_var" + fi + done + get_bottle_path_by_process_id "$PID_LIST" +} + +send_to_process() +{ + if [ -z "$2" ]; then + return 0 + fi + + for path in $(get_bottle_path_by_process_name $2); do + proc_pid=${path%=*} + prefix=${path#*=} + wine_cmd=$(get_wine_by_pid $proc_pid) + wine_name=$(echo $wine_cmd | awk -F / '{print $(NF-2)}') + if command -v $wine_name > /dev/null 2>&1; then + wine_cmd="$wine_name" + fi + echo "send to $path by $wine_cmd" + + env WINEPREFIX="$prefix" "$wine_cmd" "$SHELL_DIR/sendkeys.exe" $1 $3 + done +} + +if [ -z "$1" ]; then + echo "Please input a key [a-zA-Z]" + exit 0 +fi + +if [ -n "$2" ]; then + send_to_process $1 $2 $3 +else + send_to_process $1 "QQ|TIM" +fi