Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
Added basic keypress processing for hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
amyreese committed Jul 26, 2012
1 parent 0e7ec4b commit 8f190b0
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions pyranha/ui/gtk.py
Expand Up @@ -52,6 +52,44 @@ def process_message(self, (message_type, network, content)):
if message_type == 'stopped':
Gtk.main_quit()

def vicode(event):
"""Very simple and dirty method for generating a vi-like key binding from a given keypress event."""
k = event.hardware_keycode
v = event.keyval
m = event.state
s = event.string

result = ''

if m & Gdk.ModifierType.CONTROL_MASK:
result += 'C-'
if m & Gdk.ModifierType.SUPER_MASK:
result += 'S-'
if m & Gdk.ModifierType.META_MASK:
result += 'M-'

if (v >= 65 and v <= 90) or (v >= 97 and v <= 122) or \
(v >= 48 and v <= 57) or (v >= 33 and v <= 41):
result += chr(v)
else:
if m & Gdk.ModifierType.SHIFT_MASK:
result += '^-'

if k == 23:
result += 'Tab'
elif k == 36:
result += 'Enter'
elif k == 112:
result += 'PgUp'
elif k == 117:
result += 'PgDn'

else:
return ''

return result


class MainWindow(Gtk.Window):

def __init__(self):
Expand Down Expand Up @@ -103,17 +141,28 @@ def __init__(self):
self.command_entry.grab_focus()

def on_command_keydown(self, widget, event):
if event.keyval == 65293 and not Gdk.ModifierType.SHIFT_MASK & event.state:
code = vicode(event)

if code == '':
return False

if code == 'Enter':
text_buffer = widget.get_buffer()
command = text_buffer.get_text(text_buffer.get_start_iter(), text_buffer.get_end_iter(), True).strip()
print 'command: ', command
text_buffer.set_text('')
return True

if code == 'C-q':
self.stop(None, None)
return True

return False

def start(self, widget):
async_engine_command('connect')

def stop(self, widget, event):
def stop(self, widget=None, event=None):
async_engine_command('stop')

class MainMenu(Gtk.MenuBar):
Expand Down

0 comments on commit 8f190b0

Please sign in to comment.