[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: Pygame not handling keyboard tracking correctly



Hi all!
my code is like this

Âdef get_keys(): pygame.event.pump() #mouse buttons left-1, middle-2, right-3 for event in pygame.event.get():
#------------------- play keyboard via computer keyboard -------------------
if event.type is KEYUP:

x = (event.unicode)

if x in KEYLIST:
v.keyMIDInn = KEY_TO_MIDI_NN[x] + v.octave
player.note_on(v.keyMIDInn, 100,1) Â# play the note

for key in keyboard_full:
if v.keyMIDInn == key.midi_note_no:
key.key_select = True

if event.type is KEYUP:

x = check_chr_key(event.key)

if x in KEYLIST:
v.keyMIDInn = KEY_TO_MIDI_NN[x] + v.octave
player.note_off(v.keyMIDInn, 0,1)

for key in keyboard_full:
if v.keyMIDInn == key.midi_note_no:
key.key_select = False

v.mousepos = pygame.mouse.get_pos()
v.printnotename = [v.mousepos[0] + 12, v.mousepos[1]]



On Mon, Jun 23, 2014 at 6:09 AM, Jeffrey Kleykamp <jeffrey.kleykamp@xxxxxxxxx> wrote:
Also unicode will give you caps if you type shift or caps lock. So if you hold 'b' then hold shift and let go of 'b' I suspect you'll replicate the issue. Although it looks like the "KEYUP" events don't have a unicode so that may also be it.

It's better to use the key attribute which is independent of caps lock and shift.

Jeffrey


On Sun, Jun 22, 2014 at 8:36 PM, Jeffrey Kleykamp <jeffrey.kleykamp@xxxxxxxxx> wrote:
It may help to have less duplicate code.

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if event.type is KEYDOWN or KEYUP:
x = event.unicode

if x in KEYLIST:
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ play_number = 100 if event.type == KEYDOWN else 0
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ key_select = True if event.type == KEYDOWN else False
v.keyMIDInn = KEY_TO_MIDI_NN[x] + v.octave
player.note_on(v.keyMIDInn, play_number,1) Â# play the note

for key in keyboard_full:
if v.keyMIDInn == key.midi_note_no:
key.key_select = key_select

Also your code may run into issues if you change KEYLIST and/or keyboard_full between frames. It may also be that you're losing the screen and pressing getting the keyup event off screen where you may not get it. You can check for the status of keys using the keyboard pygame module independent of events. That'll give you the status of the key right now. Also make sure you're completely using the event buffer. It may get full. If you use event.get(type) then all the other types stay on the list. So use pump to get rid of them.

Jeffrey



On Sun, Jun 22, 2014 at 3:43 AM, diliup gabadamudalige <diliupg@xxxxxxxxx> wrote:
Dear Pygame experts/enthusiasts around the world,

Having looked at some possible solutions on stackoverflow I optimized the code to this

#------------------- play keyboard via computer keyboard -------------------

if event.type is KEYDOWN:
x = event.unicode

if x in KEYLIST:
v.keyMIDInn = KEY_TO_MIDI_NN[x] + v.octave
player.note_on(v.keyMIDInn, 100,1) Â# play the note

for key in keyboard_full:
if v.keyMIDInn == key.midi_note_no:
key.key_select = True

if event.type is KEYUP:
x = event.unicode

if x in KEYLIST:
v.keyMIDInn = KEY_TO_MIDI_NN[x] + v.octave
player.note_off(v.keyMIDInn, 0,1)

for key in keyboard_full:
if v.keyMIDInn == key.midi_note_no:
key.key_select = False

Still, if I play the keyboard really fast there are stuck MIDI notes and stuck animations.
What could be the reason?
Any help is much appreciated.



On Sun, Jun 22, 2014 at 12:59 PM, diliup gabadamudalige <diliupg@xxxxxxxxx> wrote:
Below is my code
the indention is wrong here but in the program it is correct

            if event.type is KEYDOWN:
              Âx = check_chr_key(event.key) if x in KEYLIST: if x in KEY_TO_MIDI_NN: # if key in midi note dict v.keyMIDInn = KEY_TO_MIDI_NN[x] + v.octave player.note_on(v.keyMIDInn, 100,1) # play the note
for key in keyboard_full: if v.keyMIDInn == key.midi_note_no: key.key_select = True if event.type is KEYUP: x = check_chr_key(event.key) if x in KEYLIST: v.keyMIDInn = KEY_TO_MIDI_NN[x] + v.octave player.note_off(v.keyMIDInn, 0,1) for key in keyboard_full: if v.keyMIDInn == key.midi_note_no: key.key_select = False If I play the keyboard leisurely this works fine. But If I play the keyboard really fast
sometimes the MIDI notes get stuck and so does the Animation ( the stuck MIDI note shows the keyboard note as down)
I can't see anything wrong in the code. Is it that Pygame is not scanning the keyboard fast enough? What is the work around to this?

--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions.
**********************************************************************************************




--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions.
**********************************************************************************************




--

   Jeffrey Kleykamp



--

   Jeffrey Kleykamp



--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions.
**********************************************************************************************