在 Linux 中处理 tty 中的游戏手柄按钮点击
Handling gamepad button clicks in tty, in Linux
我有一个 Linux 设备,板载一个 EmulationStation(键盘通过 USB 连接到它)。我想在终端中处理游戏手柄击键,这样我就不必为此插入键盘。
检测游戏手柄按键代码的测试脚本:
#!/bin/bash
echo 'Press button "L1":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "L1":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "L2":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "L2":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Up":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Up":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Down":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Down":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Left":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Left":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Right":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Right":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Select":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Select":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "L3":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "L3":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Start":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Start":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "X":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "X":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Y":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Y":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "B":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "B":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "A":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "A":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "R3":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "R3":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "R2":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "R2":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "R1":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "R1":' >> keycode.txt
echo "$key" >> keycode.txt
当您通过 SSH 连接或退出 EmulationStation 并启动 tty 时,脚本正常运行,当您按下游戏手柄键时,检测到键码,成功写入文件,过程完成。
如果脚本 运行 直接来自 EmulationStation,问题就开始了,显然 jstest --event /dev/input/js0
正在循环。
从 EmulationStation 运行ning 脚本的过程如下所示:
Press button "L1":
脚本不响应游戏手柄按键,或者我们看不到它。同时连接游戏手柄,使用第二个脚本我们可以从击键上看出区别:
#!/bin/bash
jstest --event /dev/input/js0
第二个脚本的输出:
Event: type 1, time -195236, number 9, value 1
Event: type 1, time -195128, number 9, value 0
Event: type 1, time -194840, number 8, value 1
Event: type 1, time -194744, number 8, value 0
Event: type 1, time -194492, number 10, value 1
Event: type 1, time -194360, number 10, value 0
Event: type 1, time -194096, number 9, value 1
Event: type 1, time -194096, number 11, value 1
Event: type 1, time -194000, number 9, value 0
Event: type 1, time -193988, number 11, value 0
显然,问题与循环 运行 无限期地使用 grep -m 1 "type 1, time .*, number .*, value 1"
不会结束这一事实有关。
当脚本不起作用时,ps aux
显示以下过程:
user 635 0.0 0.0 2048 508 ? S 03:58 0:00 sh -c sudo chmod 666 /dev/tty1; /opt/system/Scripts/Keycode.sh > /dev/tty1; printf "3c" >> /dev/tty1
user 645 0.0 0.3 6464 2916 ? S 03:58 0:00 /bin/bash /opt/system/Scripts/Keycode.sh
user 646 0.0 0.1 6464 1572 ? S 03:58 0:00 /bin/bash /opt/system/Scripts/Keycode.sh
user 647 0.0 0.0 1924 464 ? S 03:58 0:00 jstest --event /dev/input/js0
user 651 0.0 0.2 8416 2728 pts/0 R+ 03:59 0:00 ps aux
我想 运行 来自 EmulationStation 的脚本,以便它对游戏手柄按钮按下做出反应。如果有其他不使用 GUI 的方法可以做到这一点,请写出来。
我使用这个 perl module.
解决了终端中游戏手柄输入的问题
我有一个 Linux 设备,板载一个 EmulationStation(键盘通过 USB 连接到它)。我想在终端中处理游戏手柄击键,这样我就不必为此插入键盘。
检测游戏手柄按键代码的测试脚本:
#!/bin/bash
echo 'Press button "L1":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "L1":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "L2":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "L2":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Up":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Up":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Down":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Down":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Left":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Left":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Right":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Right":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Select":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Select":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "L3":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "L3":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Start":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Start":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "X":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "X":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "Y":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "Y":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "B":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "B":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "A":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "A":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "R3":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "R3":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "R2":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "R2":' >> keycode.txt
echo "$key" >> keycode.txt
echo 'Press button "R1":'
key=`jstest --event /dev/input/js0 | grep -m 1 "type 1, time .*, number .*, value 1" | awk -F ',' '{print }' | awk '{print }'`
echo 'Button "R1":' >> keycode.txt
echo "$key" >> keycode.txt
当您通过 SSH 连接或退出 EmulationStation 并启动 tty 时,脚本正常运行,当您按下游戏手柄键时,检测到键码,成功写入文件,过程完成。
如果脚本 运行 直接来自 EmulationStation,问题就开始了,显然 jstest --event /dev/input/js0
正在循环。
从 EmulationStation 运行ning 脚本的过程如下所示:
Press button "L1":
脚本不响应游戏手柄按键,或者我们看不到它。同时连接游戏手柄,使用第二个脚本我们可以从击键上看出区别:
#!/bin/bash
jstest --event /dev/input/js0
第二个脚本的输出:
Event: type 1, time -195236, number 9, value 1
Event: type 1, time -195128, number 9, value 0
Event: type 1, time -194840, number 8, value 1
Event: type 1, time -194744, number 8, value 0
Event: type 1, time -194492, number 10, value 1
Event: type 1, time -194360, number 10, value 0
Event: type 1, time -194096, number 9, value 1
Event: type 1, time -194096, number 11, value 1
Event: type 1, time -194000, number 9, value 0
Event: type 1, time -193988, number 11, value 0
显然,问题与循环 运行 无限期地使用 grep -m 1 "type 1, time .*, number .*, value 1"
不会结束这一事实有关。
当脚本不起作用时,ps aux
显示以下过程:
user 635 0.0 0.0 2048 508 ? S 03:58 0:00 sh -c sudo chmod 666 /dev/tty1; /opt/system/Scripts/Keycode.sh > /dev/tty1; printf "3c" >> /dev/tty1
user 645 0.0 0.3 6464 2916 ? S 03:58 0:00 /bin/bash /opt/system/Scripts/Keycode.sh
user 646 0.0 0.1 6464 1572 ? S 03:58 0:00 /bin/bash /opt/system/Scripts/Keycode.sh
user 647 0.0 0.0 1924 464 ? S 03:58 0:00 jstest --event /dev/input/js0
user 651 0.0 0.2 8416 2728 pts/0 R+ 03:59 0:00 ps aux
我想 运行 来自 EmulationStation 的脚本,以便它对游戏手柄按钮按下做出反应。如果有其他不使用 GUI 的方法可以做到这一点,请写出来。
我使用这个 perl module.
解决了终端中游戏手柄输入的问题