From 054da4e69170e51c56b3fe1d199cb7d26356d2c6 Mon Sep 17 00:00:00 2001 From: Tobias Dammers Date: Sun, 18 Mar 2018 20:10:23 +0100 Subject: [PATCH] Fix: chat messages from MP list captured kbd forever --- Nasal/multiplayer.nas | 47 +++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/Nasal/multiplayer.nas b/Nasal/multiplayer.nas index b58e35cc9..00ddd13c7 100644 --- a/Nasal/multiplayer.nas +++ b/Nasal/multiplayer.nas @@ -78,20 +78,43 @@ var prefix = "Chat Message:"; var input = ""; var kbdlistener = nil; +var my_kbd_listener = func(event) +{ + var key = event.getNode("key"); + + # Only check the key when pressed. + if (!event.getNode("pressed").getValue()) + return; + + if (handle_key(key.getValue())) + key.setValue(-1); # drop key event +} + +var capture_kbd = func() +{ + if (kbdlistener == nil) { + kbdlistener = setlistener("/devices/status/keyboard/event", my_kbd_listener); + } +} + +var release_kbd = func() +{ + if (kbdlistener != nil) { + removelistener(kbdlistener); + kbdlistener = nil; + } +} var compose_message = func(msg = "") { input = prefix ~ msg; gui.popupTip(input, 1000000); + capture_kbd(); +} - kbdlistener = setlistener("/devices/status/keyboard/event", func (event) { - var key = event.getNode("key"); - # Only check the key when pressed. - if (!event.getNode("pressed").getValue()) - return; - - if (handle_key(key.getValue())) - key.setValue(-1); # drop key event - }); +var end_compose_message = func() +{ + gui.popdown(); + release_kbd(); } var handle_key = func(key) @@ -104,8 +127,7 @@ var handle_key = func(key) input = substr(input, size(prefix)); # Send the message and switch off the listener. setprop("/sim/multiplay/chat", input); - gui.popdown(); - removelistener(kbdlistener); + end_compose_message(); return 1; } elsif (key == 8) @@ -124,8 +146,7 @@ var handle_key = func(key) elsif (key == 27) { # escape -> cancel - gui.popdown(); - removelistener(kbdlistener); + end_compose_message(); return 1; } elsif ((key > 31) and (key < 128))