简体   繁体   中英

Sending keyboard typed data to Webpage using flask SSE

I am trying to send Keyboard typed data to a webpage using Flask SSE following this tutorial . I could send the data. But the problem is that I could send the data only when the cursor is focused on the terminal (I am using input() method in an infinite loop to capture data).

Since I want to capture data even though the cursor is not focused in the terminal I tried using the keyboard module ( record() method). Though I could successfully run and capture in a separate program, I couldnt run it in Flask code. I am running the following code.

import gevent
from gevent.pywsgi import WSGIServer
from gevent import monkey
monkey.patch_all()
from numpy import random
from flask import Flask, json, Response, render_template
import keyboard
import sys


app = Flask(__name__)

def event():
    while True:
        print("Inside")
        recorded_events =  keyboard.read_key(suppress=True);

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/stream/', methods=['GET', 'POST'])
def stream():
    return Response(event(), mimetype="text/event-stream")

if __name__ == "__main__":
    WSGIServer(('', 5001), app).serve_forever()

Aren't you supposed to yield the values?

def event():
    while True:
        yield keyboard.read_key(suppress=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM