Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Nodes to use the keyboard state #942

Merged
merged 1 commit into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Release History
0.4.4 (unreleased)
==================

- Improvement (Experimental): Nodes have access to the GUI keyboard state
- Improvement: support for nengo-bio Connections
- Improvement: show multiple connections between objects
- Bugfix: handle new Connection transform in Nengo 3.0.0
Expand Down
11 changes: 11 additions & 0 deletions nengo_gui/components/netgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ def message(self, msg):
return
action = info.get('act', None)
undo = info.get('undo', None)
event = info.get('event', None)
if action is not None:
del info['act']
if action in ('auto_expand', 'auto_collapse'):
Expand All @@ -416,6 +417,8 @@ def message(self, msg):
self.undo()
else:
self.redo()
elif event is not None:
self.handle_event(event, info)
else:
print('received message', msg)

Expand Down Expand Up @@ -718,3 +721,11 @@ def create_connection(self, client, conn, parent):
info = dict(uid=uid, pre=pres, post=posts, type='conn', parent=parent,
kind=kind)
client.write_text(json.dumps(info))

def handle_event(self, event, info):
if event == 'keyup':
self.page.keys_pressed.discard(info['key'])
self.page.key_codes_pressed.discard(info['keyCode'])
elif event == 'keydown':
self.page.keys_pressed.add(info['key'])
self.page.key_codes_pressed.add(info['keyCode'])
4 changes: 4 additions & 0 deletions nengo_gui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def __init__(self, gui, filename, settings, reset_cfg=False):
self.config_save_time = None # time of last config file save
self.config_save_period = 2.0 # minimum time between saves

self.keys_pressed = set()
self.key_codes_pressed = set()

self.lock = threading.Lock()

# use the default filename if none is given
Expand Down Expand Up @@ -211,6 +214,7 @@ def execute(self, code):
code_locals = {}
code_locals['nengo_gui'] = nengo_gui
code_locals['__file__'] = self.filename
code_locals['__page__'] = self

self.code = code
self.error = None
Expand Down
67 changes: 49 additions & 18 deletions nengo_gui/static/hotkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,7 @@ Nengo.Hotkeys = function () {
var is_editable = (ev.target.tagName === 'INPUT' ||
ev.target.tagName == 'TEXTAREA');

if (typeof ev.key != 'undefined') {
var key = ev.key;
} else {
switch (ev.keyCode) {
case 191:
var key = '?';
break;
case 8:
var key = 'backspace';
break;
case 13:
var key = 'enter';
break;
default:
var key = String.fromCharCode(ev.keyCode)
}
}
var key = key.toLowerCase();
var key = self.determine_key(ev);
var ctrl = ev.ctrlKey || ev.metaKey;

// toggle editor with ctrl-e
Expand Down Expand Up @@ -93,8 +76,33 @@ Nengo.Hotkeys = function () {
Nengo.ace.update_trigger = true;
ev.preventDefault();
}

if (!is_editable) {
Nengo.netgraph.ws.send(JSON.stringify(
{event:'keydown',
keyCode:ev.keyCode,
key:key
}));
}
}
});

document.addEventListener('keyup', function(ev) {
if (self.active) {

var is_editable = (ev.target.tagName === 'INPUT' ||
ev.target.tagName == 'TEXTAREA');

if (!is_editable) {
Nengo.netgraph.ws.send(JSON.stringify(
{event:'keyup',
keyCode:ev.keyCode,
key:self.determine_key(ev)
}));
}
}
});

}

Nengo.Hotkeys.prototype.callMenu = function () {
Expand All @@ -111,4 +119,27 @@ Nengo.Hotkeys.prototype.set_active = function(bool) {
this.active = bool;
}

Nengo.Hotkeys.prototype.determine_key = function(ev) {
if (typeof ev.key != 'undefined') {
var key = ev.key;
} else {
switch (ev.keyCode) {
case 191:
var key = '?';
break;
case 8:
var key = 'backspace';
break;
case 13:
var key = 'enter';
break;
default:
var key = String.fromCharCode(ev.keyCode)
}
}

var key = key.toLowerCase();
return key;
}

Nengo.hotkeys = new Nengo.Hotkeys();