Python Forum
how to pause pyxhook keylogger - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to pause pyxhook keylogger (/thread-10923.html)



how to pause pyxhook keylogger - johweb - Jun-13-2018

Hello everybody.

New to python, I'm making a little application, with a keyboard listener (pyxhook). When user press "Enter" key, a video is shown on screen, but if the user press again "Enter", it's waiting for video to stop and then launched again...
How can I make the listener to pause until the video has ended ?

Here's my code :
#!/usr/bin/env python

import pygame
import sys
import time
import subprocess
import os
import requests
import youtube_dl
import pyxhook
import _thread
from pygame.locals import *

# MAXIMUM picture display sizes
max_width = 1600
max_height = 960
width = 0
height = 0

#color
black = (0,0,0)

pygame.init()     

def image(imagefile):
    image = pygame.image.load(imagefile)
    size = image.get_rect()
    width = size[2]
    height = size[3]
    windowSurfaceObj = pygame.display.set_mode((width,height),pygame.FULLSCREEN)
    windowSurfaceObj.blit(image,(0,0))
    pygame.display.update()

def video(myvideo):
    global black
    global videoRunning

    image("/home/pi/wait.png")
	command = 'youtube-dl --no-warnings -f mp4 -g ' + myvideo
	url = subprocess.check_output(command.split(), shell=False)
	realurl = url.decode("utf-8", "strict")[:-1]
	command = 'omxplayer -o local -b --no-osd --vol -2000 ' + realurl
	windowSurfaceObj = pygame.display.set_mode((width,height),pygame.FULLSCREEN)
	windowSurfaceObj.fill(black)
	pygame.display.update()
	video = subprocess.run(command.split(), shell=False)
	videoRunning = False
        
def kbevent(event):
    global videoRunning
    
    if event.Ascii ==13:
        if videoRunning:
            print("nothing, video running")
        else:
            print("run video")
            videoRunning = True
            myvideo='https://www.youtube.com/watch?v=B7bqAsxee4I'
            video(myvideo)
            image("/home/pi/home.png")
    elif event.Ascii ==27:
        if videoRunning:
            print("stop video")
            videoRunning=False
            os.system('killall omxplayer.bin')
            image("/home/pi/home.png")
        else:
            print("quit program")
            pygame.quit()
            sys.exit()
    else:
        pass

def listener():
    global running
    
    hookman = pyxhook.HookManager()
    hookman.KeyDown = kbevent
    hookman.HookKeyboard()
    hookman.start()
    running = True
    while running:
        time.sleep(0.1)
    hookman.cancel()

videoRunning=False
_thread.start_new_thread(listener, ())
image("/home/pi/home.png")