Page 1 of 1

Keystroke detection without drawing a window...

Posted: Tue Jul 03, 2012 9:22 pm
by Fugazi1978
I'm trying to get keystroke detection without a window (I want a purely command line driven jukebox).
My code is poor and I'm trying to code without using def etc:

Code: Select all

if pygame.event.get(pygame.KEYDOWN) == pygame.K_b:
	pygame.mixer.music.pause()
if event.key == pygame.K_n:
	pygame.mixer.music.stop()
if event.key == pygame.K_v:
	track = track - 2
Basically I want to detect a keystroke and do something dependent on the key.

Any ideas? This is without drawing a window etc, this is purely command line driven.
pyglet or pygame or purely python or ANY other source that'll run on Linux (debian).

Thanks.

Re: Keystroke detection without drawing a window...

Posted: Tue Jul 03, 2012 10:18 pm
by jmacey
you could try curses which is installed with the default python, something like this

Code: Select all

import curses
#init the curses screen
stdscr = curses.initscr()
#use cbreak to not require a return key press
curses.cbreak()
print "press q to quit"
quit=False
# loop
while quit !=True:
	c = stdscr.getch()
	print curses.keyname(c),
	if curses.keyname(c)=="q" :
		quit=True

curses.endwin()
http://docs.python.org/library/curses.html

Re: Keystroke detection without drawing a window...

Posted: Wed Jul 04, 2012 7:34 am
by tcstyle
Pygame is a good choice for this task. It can run without display and the event manager should handle your specified events.
Example:

Code: Select all

import pygame
pygame.init()

finished = False

while not finished:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      finished = True
   if event.type == pygame.KEYDOWN:
      if event.key == #specify your keys and actions here
#...
This should be your main loop for catching the key events. (Please correct the indents when copying this code snippet.)

Re: Keystroke detection without drawing a window...

Posted: Wed Jul 04, 2012 2:47 pm
by Fugazi1978
Here's the code for the media player with keystroke detection, unfortunately it plays slowly (anyone any ideas):

[code]
import os
import sys
import pygame
import curses
#pygame.init()
loop = 0
esc = 0
os.system('clear')

# music files (ogg) must be in a folder, 'music', in same folder as program
os.system('ls -1pR music > files.out')

# open list of files
f = open('files.out', 'r')

# create a list C[ ]
C = ['this','is','a','list']

# empty c
C[:] = []

# read first line of files.out
A = f.readline()

# reads all lines of files.out, places full path to each file in C[ ]
while esc == 0:
if A[-2:-1] == ":":
while loop == 0:
B = f.readline()
if B[-2:-1] == ":":
A = B
if B[-5:-1] == ".ogg":
C[len(C)+1:] = [A[:-2] + '/' + B[:-1]]
if B == "":
esc = 1
loop = 1
else:
A = f.readline()

#init the curses screen
stdscr = curses.initscr()

#use cbreak to not require a return key press
curses.cbreak()

track = 0
breaker = 0

# set up the mixer
freq = 44100 # audio CD quality
bitsize = -16 # unsigned 16 bit
channels = 2 # 1 is mono, 2 is stereo
buffer = 2048 # number of samples (experiment to get right sound)
pygame.mixer.init(freq, bitsize, channels, buffer)

# optional volume 0 to 1.0
pygame.mixer.music.set_volume(1.0)

# starts pygame clock
clock = pygame.time.Clock()

while breaker == 0:
pygame.mixer.music.load(C[track])
pygame.mixer.music.play()
pz = 1
# check if playback has finished
while pygame.mixer.music.get_busy():
clock.tick(30)
# python curses to 'get' keyboard input
k = stdscr.getch()
# press b to pause/play track
if curses.keyname(k)=="b":
if pz == 1:
pygame.mixer.music.pause()
pz = 2
else:
pygame.mixer.music.play()
pz = 1
# press n to move to next track
if curses.keyname(k)=="n":
pygame.mixer.music.stop()
# press v to go back a track
if curses.keyname(k)=="v":
track = track -2
pygame.mixer.music.stop()
# press q to quit
if curses.keyname(k)=="q":
pygame.mixer.quit()
breaker = 1
curses.endwin()
track = track + 1

# check track number isn't less than first track
if track < 0:
track = len(C) + track

# check track number isn't more than last track
if track > len(C) - 1:
track = 0
[/code]

It plays ogg files, anywhere from within the 'music' folder that is in the same folder as the program.

Any improvements to the current script greatly appreciated. Requires pygame.

Aim of the program is a command line jukebox with keystroke detection for moving through tracks.

Matthew

Re: Keystroke detection without drawing a window...

Posted: Thu Jul 05, 2012 2:02 pm
by Yam
If you dont draw a window you need to manually 'pump' the event que
Its in the documentation pygame.event.pump()

http://www.pygame.org/docs/ref/event.ht ... event.pump

Re: Keystroke detection without drawing a window...

Posted: Fri Jul 06, 2012 6:46 pm
by Yam
Been experimenting today can't really get the pump to work.
Its kinda hinted that its not a good idea to try so the other suggestions are proably a better idea.

I'm guessing the pump in conjuction with pygame.event.set_grab / get_grab will probably work but i dont even want to think about the problems that might cause.

Re: Keystroke detection without drawing a window...

Posted: Sat Jul 07, 2012 12:22 pm
by Fugazi1978
Curses is brilliant in conjunction with the pygame check for end of track.