diff options
| author | troido <troido@hotmail.com> | 2018-01-03 01:17:45 +0100 |
|---|---|---|
| committer | troido <troido@hotmail.com> | 2018-01-03 01:17:45 +0100 |
| commit | 5da66af8dbce7cc5c08d142591907e8474a5bbda (patch) | |
| tree | 5038b0f2f2940e32aaa42897a4bb41e9c2922021 /asciifarm | |
| parent | f3affd98f361e391b32b52e060fda7c53feef574 (diff) | |
inputhanding now handles keyname lookup. enter is now also named
Diffstat (limited to 'asciifarm')
| -rw-r--r-- | asciifarm/client/gameclient.py | 14 | ||||
| -rw-r--r-- | asciifarm/client/inputhandling.hy | 26 |
2 files changed, 21 insertions, 19 deletions
diff --git a/asciifarm/client/gameclient.py b/asciifarm/client/gameclient.py index 6a80572..7652f66 100644 --- a/asciifarm/client/gameclient.py +++ b/asciifarm/client/gameclient.py @@ -105,17 +105,21 @@ class Client: with(open(self.logFile, 'a')) as f: f.write(text+'\n') + def nameFromKey(self, keynum): # this probably belongs in inputhandler... + prenamed = { + 10: "KEY_ENTER" + } + if keynum in prenamed: + return prenamed[keynum] + return str(curses.keyname(keynum), "utf-8") + def command_loop(self): while self.keepalive: key = self.stdscr.getch() if key == 27: self.keepalive = False return - try: - keyname = str(curses.keyname(key), "utf-8") - except ValueError: - continue - self.inputHandler.onKey(keyname) + self.inputHandler.onKey(key) diff --git a/asciifarm/client/inputhandling.hy b/asciifarm/client/inputhandling.hy index 80043c9..b006073 100644 --- a/asciifarm/client/inputhandling.hy +++ b/asciifarm/client/inputhandling.hy @@ -1,18 +1,15 @@ (require [asciifarm.client.keymacros [*]]) +(import [curses]) -(defmacro eval-in-context [code] - `( - (eval `(do - (require [asciifarm.client.keymacros [*]]) - (fn [client display connection] - ~~code))) - self.client - self.display - self.connection)) +(setv prenamedkeys { ; or should this be def? + 10 "KEY_ENTER" +}) -(defmacro sendinput [message] `( - self.client.send ["input" ~message])) +(defn nameFromKey [key] + (if (in key prenamedkeys) + (get prenamedkeys key) + (str (curses.keyname key) "utf-8"))) (defclass InputHandler [] @@ -38,7 +35,7 @@ (try (eval (read-str (+ "(" commandstring ")"))) (except [e Exception] - (self.display.addMessage (repr e))))) + (self.client.log (repr e))))) (defn parseMessage [self message] (if message @@ -53,6 +50,7 @@ (defn getDocs [self] (if (in "help" self.commands) ((get self.commands "help")) "")) - (defn onKey [self key] - (if (in key self.commands) ((get self.commands key)))) + (defn onKey [self key] (do + (setv keyname (nameFromKey key)) + (if (in keyname self.commands) ((get self.commands keyname))))) ) |
