1

I am trying to detect the key pressed by a user in order to reset a variable for my program, a basic application to detect a barcode, which is running inside a While true

I have tried using keyboard library like in the first solution suggested here

import keyboard  # using module keyboard
while True:  
        if keyboard.is_pressed('q'):  # if key 'q' is pressed 
            print('You Pressed A Key!')
        ... 
       

but I m getting an error You must be root to use this library on Linux. I am not sure if that it means is not possible to use this approach in my case because I m ruining the program on Raspberry Pi OS.

I also tried the second approach ( from the same link) using pynput.keyboard but as the author says, it does not perform well when using inside a loop. the application waits for a key to be pressed.

Edit : More Info

from pyzbar import pyzbar
import datetime
import imutils
import time
from imutils.video import VideoStream
import pika
import json

from pynput.keyboard import Key, Listener

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host="----",
                              virtual_host="----",
                              credentials=pika.credentials.PlainCredentials(
                                  "---", "---")
                              ))
channel = connection.channel()
channel.queue_declare(queue='barcode_queue')

print("[INFO] Connection Started ...")

vs = VideoStream(usePiCamera=True).start()
print("[INFO] starting video stream...")

time.sleep(2.0)

userWebsocket = None

while True:
      
    # this is what I want to achive
    # key = Get the key pressed
    # if key == 'q' then userWebsocket = None
        
    
    frame = vs.read()
    frame = imutils.resize(frame, width=400)

    barcodes = pyzbar.decode(frame)
    for barcode in barcodes:
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type

        if barcodeType == 'QRCODE':
            decodedBarcode = json.loads(barcodeData)
            print(decodedBarcode)
            print(decodedBarcode['name'])

            userWebsocket = decodedBarcode['id']

        else:
            print(datetime.datetime.now(), barcodeData, barcodeType)
            if (userWebsocket is None):
                print("please login before")
            else:
                sentObj = {"socket": userWebsocket, "barcode": barcodeData}
                jsonSentObj = json.dumps(sentObj)
                channel.basic_publish(exchange='', routing_key='barcode_queue', body=jsonSentObj)
        time.sleep(5)

vs.stop()

What I want to achieve is to "sign-out" the user when he presses a certain key.

Cristian Flaviu
  • 275
  • 2
  • 7
  • 18
  • https://github.com/boppreh/keyboard/issues?q=is%3Aissue+must+be+root+is%3Aclosed+ — You don’t need to (and *should not*) use a key logger library to detect key presses inside your application. https://stackoverflow.com/a/47197390/1968 – Konrad Rudolph Jun 24 '21 at 11:49
  • Maybe it would work if you ran your program as root. type into terminal: "sudo python3 [name of the python file]". – user2952903 Jun 24 '21 at 11:59
  • @KonradRudolph It acts as the second method I have tried. It detects the keys but it prevents the rest of the code from the loop to be executed. – Cristian Flaviu Jun 24 '21 at 12:03
  • @CristianFlaviu, Did you try running it with sudo? – user2952903 Jun 24 '21 at 12:14
  • @user2952903 That's really not an appropriate solution. – Konrad Rudolph Jun 24 '21 at 12:29
  • @KonradRudolph, I think it is impossible to get info about pressed keys while the application window is not active without being root. From the post I am not sure if he wants to detect keys only while window is active or all the time. – user2952903 Jun 24 '21 at 12:56
  • @user2952903 “while the application window is not active” is the key point here. What makes you assume OP wants to do that? Nothing in the question indicates it. – Konrad Rudolph Jun 24 '21 at 12:57
  • @KonradRudolph, If he want to detect keys only while window is active, then he should use some other module. OP using this module(keyboard) made me think that he he wants to detect keys all the time, but maybe he just misunderstood what this module does. – user2952903 Jun 24 '21 at 13:06
  • @user2952903 Sorry for the late response, the application inside a PyCharm environment. Do I need to install all the libraries locally in order to run it with sudo python myFile.py ? – Cristian Flaviu Jun 24 '21 at 14:57
  • @KonradRudolph I pasted the whole code and also what I m trying to achieve. The idea is that a user( customer) should be able to connect to the application using a QR Code and after that he can scan a product barcode and do other stuff on another Web Application. What I want to achieve is to "sign-out" the user when pressing a certain key. – Cristian Flaviu Jun 24 '21 at 15:01

0 Answers0