From 1a816166b629637d0277a56395cdaa1aebf89ecc Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 15 Oct 2021 20:23:11 -0400 Subject: [PATCH] command_prompt: ignore numlock and such --- command_prompt.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/command_prompt.py b/command_prompt.py index 72c7006..ab1538b 100644 --- a/command_prompt.py +++ b/command_prompt.py @@ -29,6 +29,7 @@ class Command_prompt(Label): self.draw() + def draw(self): with self.canvas.before: # background @@ -44,9 +45,13 @@ class Command_prompt(Label): else: self.text=self.raw_text + def on_key_down(self, keyboard, keycode, text, modifiers): print(keycode,text,modifiers) if self.insert: + # process modifiers + mods=self.process_modifiers(modifiers) + # process command if keycode[1]=="enter": self.insert=False @@ -58,9 +63,9 @@ class Command_prompt(Label): self.move_cursor_relative(-1) elif keycode[1]=="right": self.move_cursor_relative(1) - elif len(modifiers)==1 and modifiers[0]=="ctrl" and text=="a": + elif mods==['c'] and text=="a": self.move_cursor(1) - elif len(modifiers)==1 and modifiers[0]=="ctrl" and text=="e": + elif mods==['c'] and text=="e": self.move_cursor(len(self.raw_text)) # delete @@ -68,7 +73,7 @@ class Command_prompt(Label): self.backspace() elif keycode[1]=="delete": self.delete_forward() - elif len(modifiers)==1 and modifiers[0]=="ctrl" and text=="k": + elif mods==['c'] and text=="k": self.delete_all_forward() def on_textinput(self,window,text): @@ -79,6 +84,30 @@ class Command_prompt(Label): self.append_text_at_cursor(text) self.insert=True + + # get modifiers from list + # returns an alphabetically ordered array of characters + # 'a' is alt + # 'c' is ctrl + # 'm' is meta + # 's' is shift + # ignore all others + def process_modifiers(self,modifiers): + out=[] + for mod in modifiers: + if mod=="alt": + out.append('a') + elif mod=="ctrl": + out.append('c') + elif mod=="meta": + out.append('m') + elif mod=="shift": + out.append('s') + out.sort() + return out + + + # append to text at position of cursor def append_text_at_cursor(self,text): if self.cursor==len(self.raw_text): @@ -121,6 +150,7 @@ class Command_prompt(Label): self.cursor=len(text) self.draw() + # move cursor n steps to right (left if negative) def move_cursor_relative(self,n): if n==0 or (n>0 and self.cursor==len(self.raw_text)) or (n<0 and self.cursor==0): @@ -133,6 +163,7 @@ class Command_prompt(Label): self.cursor=max(1,min(len(self.raw_text),n)) self.draw() + # parse text as argv def parse_command(self): # init