1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-15 23:46:11 +00:00

fix: improve input handling for mouse wheel events (#49)

This commit is contained in:
Carolyn Sun
2025-11-15 10:10:44 +08:00
committed by GitHub
parent 1cb87c67e8
commit 9b6eee968e
2 changed files with 36 additions and 10 deletions

View File

@@ -330,8 +330,21 @@ read_key() {
else else
echo "QUIT" # ESC [ timeout echo "QUIT" # ESC [ timeout
fi fi
elif [[ "$rest" == "O" ]]; then
# Application keypad mode sequences (mouse wheel often generates these)
if IFS= read -r -s -n 1 -t 1 rest2 2> /dev/null; then
case "$rest2" in
"A") echo "UP" ;; # ESC O A
"B") echo "DOWN" ;; # ESC O B
"C") echo "RIGHT" ;; # ESC O C
"D") echo "LEFT" ;; # ESC O D
*) echo "OTHER" ;; # Ignore other ESC O sequences
esac
else else
echo "QUIT" # ESC + something else echo "OTHER" # ESC O timeout
fi
else
echo "OTHER" # ESC + something else (not [ or O)
fi fi
else else
# ESC pressed alone - treat as quit # ESC pressed alone - treat as quit
@@ -351,12 +364,17 @@ read_key() {
# Drain pending input (useful for scrolling prevention) # Drain pending input (useful for scrolling prevention)
drain_pending_input() { drain_pending_input() {
local drained=0 local drained=0
# Single pass with reasonable timeout # Multiple passes with very short timeout to catch mouse wheel bursts
# Touchpad scrolling can generate bursts of arrow keys # Mouse wheel scrolling can generate rapid sequences like B^[OB^[OB^[O...
while IFS= read -r -s -n 1 -t 0.01 _ 2> /dev/null; do
((drained++))
# Higher safety limit for mouse wheel sequences
[[ $drained -gt 1000 ]] && break
done
# Second pass with even shorter timeout to catch any remaining input
while IFS= read -r -s -n 1 -t 0.001 _ 2> /dev/null; do while IFS= read -r -s -n 1 -t 0.001 _ 2> /dev/null; do
((drained++)) ((drained++))
# Safety limit to prevent infinite loop [[ $drained -gt 1500 ]] && break
[[ $drained -gt 500 ]] && break
done done
} }

View File

@@ -27,12 +27,18 @@ _pm_parse_csv_to_array() {
done done
} }
# Non-blocking input drain (bash 3.2) # Non-blocking input drain (bash 3.2) - improved for mouse wheel
drain_pending_input() { drain_pending_input() {
local _k local _k drained=0
# -t 0 is non-blocking; -n 1 consumes one byte at a time # Multiple passes to handle mouse wheel burst sequences
while IFS= read -r -s -n 1 -t 0 _k; do while IFS= read -r -s -n 1 -t 0.01 _k 2> /dev/null; do
IFS= read -r -s -n 1 _k || break ((drained++))
[[ $drained -gt 1000 ]] && break
done
# Second pass with shorter timeout
while IFS= read -r -s -n 1 -t 0.001 _k 2> /dev/null; do
((drained++))
[[ $drained -gt 1500 ]] && break
done done
} }
@@ -488,6 +494,8 @@ paginated_multi_select() {
# Main interaction loop # Main interaction loop
while true; do while true; do
draw_menu draw_menu
# Drain any pending input to prevent mouse wheel scroll issues
drain_pending_input
local key local key
key=$(read_key) key=$(read_key)