Handling Mouse Events

Handling Mouse Events

This Node-Red flow is part of the Collection entitled “Fuzzy shell for developing a custom EEG BCI”. The flow illustrates how to handle mouse events relative to the active (current) window by a Python script in Node-RED. It is used to discriminate the current stimulus that depends on whether the mouse is moving or not. Processing the response of the mouse event is up to the user. Combining with the FIS context allows for event-related brain activity interpretation.

How to import the flow

This flow requires a node to interact with a python process from Node-RED. It executes a python script “mouseEvent.py” from Node-RED. Input to the node will become the argument for the python script, output of the script will be sent to the output of the node. 1.If you are not already installed - go to your command prompt and run:

$ npm install node-red-contrib-pythonshell

2.The mouse events (handling by a Python script in Node-RED) are used for identifying the current condition and depends on whether the mouse is moving or not.

The Python Script:

from pynput.mouse import Listener

import logging

import sys

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

def on_move(x, y): p = x+y

#output of the script will be sent to output of the node-red

print(p)

#logging.info("Mouse moved to ({0}, {1})".format(x, y))

def on_click(x, y, button, pressed): if pressed: print('Pressed'); else: print('Released');

def on_scroll(x, y, dx, dy): listener.stop()

with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener: listener.join()

3.Start Node-RED in your command prompt.

4.On your web-browser, open Node-RED on http://127.0.0.1:1880

5.On the top-right screen, go to Menu/Import/Clipboard and paste the contents of the JSON file below in "example directory".

6.Wire the “link in” node to the “link out” node in the flow entitled “Data registered by EPOC”.

7.The Python shell handles the mouse events for identifying whether the mouse is moving or not. Edit the Python node parameters: Provide a python file with full path; check the “Continuous”; check the “Stdin Input”.

8.The flow can also run individually outside of this Collection by connecting the python node to an “inject” node.

9.Deploy the flow.

[{"id":"14485c95.b0a353","type":"inject","z":"558e15ac.709d2c","name":"ON","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":"","x":190,"y":80,"wires":[["da284b4d.9065a8"]]},{"id":"68d56f73.800f6","type":"inject","z":"558e15ac.709d2c","name":"OFF","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":"","x":190,"y":120,"wires":[["da284b4d.9065a8"]]},{"id":"da284b4d.9065a8","type":"change","z":"558e15ac.709d2c","name":"Enable / Disable EMOTIV flow","rules":[{"t":"set","p":"start-stop","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"Mouse","pt":"global","to":"0","tot":"num"},{"t":"set","p":"Mouse_prev","pt":"global","to":"0","tot":"num"},{"t":"set","p":"Mouse_start","pt":"global","to":"null","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":120,"wires":[["659d66.315a829c"]]},{"id":"659d66.315a829c","type":"debug","z":"558e15ac.709d2c","name":"","active":true,"tosidebar":true,"console":false,"complete":"false","x":630,"y":120,"wires":[]},{"id":"2e1dede9.728fd2","type":"function","z":"558e15ac.709d2c","name":"event BROKER","func":" \n  var mouse=context.global.get('Mouse');\n  var mouse_prev=context.global.get('Mouse_prev');\n  \n  var msg1 ={};\n  var msg2 ={};\n  \n\n\nif(mouse!==mouse_prev) {\n   context.global.set('bot_up','visual');\n   context.global.set('top_down','null');\n   context.global.set('Mouse_prev', mouse);\n   msg1.payload=\"mouse_moving\";\n   return msg1;\n   }\n\n if(mouse===mouse_prev) {\n\n context.global.set('top_down','cognitive');\n context.global.set('bot_up','null');\n msg2.payload=\"mouse_stoped\";\n  return msg2;\n }\n\n//return [ [ msg1 ], [msg2 ]];\n   \n\n\n\n","outputs":2,"noerr":0,"x":200,"y":300,"wires":[["74261106.f0016"],["dc43fc17.8d427"]]},{"id":"1305c27b.e845be","type":"pythonshell in","z":"558e15ac.709d2c","name":"get move","pyfile":"/Users/User/.../mouseEvent.py","virtualenv":"","continuous":true,"stdInData":true,"x":200,"y":220,"wires":[["54c53cd4.655654"]]},{"id":"54c53cd4.655654","type":"function","z":"558e15ac.709d2c","name":"set mouse","func":"context.global.set('Mouse_start', msg.payload );\ncontext.global.set('Mouse', parseInt(msg.payload) );\n\n msg.payload=context.global.get('Mouse');\nreturn msg;\n\n","outputs":1,"noerr":0,"x":390,"y":220,"wires":[["a3d145c8.7f5fd8"]]},{"id":"a3d145c8.7f5fd8","type":"debug","z":"558e15ac.709d2c","name":"","active":false,"tosidebar":true,"console":false,"complete":"false","x":570,"y":220,"wires":[]},{"id":"74261106.f0016","type":"debug","z":"558e15ac.709d2c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":280,"wires":[]},{"id":"dc43fc17.8d427","type":"debug","z":"558e15ac.709d2c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":320,"wires":[]},{"id":"3b111327.ae434c","type":"link in","z":"558e15ac.709d2c","name":"LINK2 start EMOTIV data here","links":["98f42b5b.1b8488"],"x":335,"y":160,"wires":[["1305c27b.e845be","2e1dede9.728fd2"]]}]

Flow Info

Created 4 years, 6 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • change (x1)
  • debug (x4)
  • function (x2)
  • inject (x2)
  • link in (x1)
Other

Tags

  • EmotivBCI Node-RED Toolbox, Handling Mouse Events, Python Shell
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option