command_prompt: ignore numlock and such

This commit is contained in:
Ian Jauslin 2021-10-15 20:23:11 -04:00
parent b8390bfac8
commit 1a816166b6

View File

@ -29,6 +29,7 @@ class Command_prompt(Label):
self.draw() self.draw()
def draw(self): def draw(self):
with self.canvas.before: with self.canvas.before:
# background # background
@ -44,9 +45,13 @@ class Command_prompt(Label):
else: else:
self.text=self.raw_text self.text=self.raw_text
def on_key_down(self, keyboard, keycode, text, modifiers): def on_key_down(self, keyboard, keycode, text, modifiers):
print(keycode,text,modifiers) print(keycode,text,modifiers)
if self.insert: if self.insert:
# process modifiers
mods=self.process_modifiers(modifiers)
# process command # process command
if keycode[1]=="enter": if keycode[1]=="enter":
self.insert=False self.insert=False
@ -58,9 +63,9 @@ class Command_prompt(Label):
self.move_cursor_relative(-1) self.move_cursor_relative(-1)
elif keycode[1]=="right": elif keycode[1]=="right":
self.move_cursor_relative(1) 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) 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)) self.move_cursor(len(self.raw_text))
# delete # delete
@ -68,7 +73,7 @@ class Command_prompt(Label):
self.backspace() self.backspace()
elif keycode[1]=="delete": elif keycode[1]=="delete":
self.delete_forward() self.delete_forward()
elif len(modifiers)==1 and modifiers[0]=="ctrl" and text=="k": elif mods==['c'] and text=="k":
self.delete_all_forward() self.delete_all_forward()
def on_textinput(self,window,text): def on_textinput(self,window,text):
@ -79,6 +84,30 @@ class Command_prompt(Label):
self.append_text_at_cursor(text) self.append_text_at_cursor(text)
self.insert=True 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 # append to text at position of cursor
def append_text_at_cursor(self,text): def append_text_at_cursor(self,text):
if self.cursor==len(self.raw_text): if self.cursor==len(self.raw_text):
@ -121,6 +150,7 @@ class Command_prompt(Label):
self.cursor=len(text) self.cursor=len(text)
self.draw() self.draw()
# move cursor n steps to right (left if negative) # move cursor n steps to right (left if negative)
def move_cursor_relative(self,n): 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): 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.cursor=max(1,min(len(self.raw_text),n))
self.draw() self.draw()
# parse text as argv # parse text as argv
def parse_command(self): def parse_command(self):
# init # init