mr.fuji
mr.fuji

Reputation: 11

In Pure Data how to keyup, keydown, and while keydown?

I'm trying to setup a little midi keyboard (using my computer's keyboard) in Pure Data. It works this way:

press a key > send a note_on on midi channel
stop pressing a key > send a note_off on midi channel

The problem is, that when you keep a key pressed the [key] object generates a series of inputs instead of a single (long) one. This stops the (desired) note from playing (since the original input stops, after ~500ms) and re-starts playing the note many times in a row.

I've already tried [change], [timer]+[moses] and other non-solutions, I'm looking for a better implementation of [key] that can handle long key-presses

I'm looking for something that does [key]'s job but that can handle a long-press, if I long-press a key with [key] for more than a second it does something like: key....(1 sec passes)...keyup.key.keyup.key.keyup. and it goes on and on...

Upvotes: 1

Views: 5977

Answers (2)

umläute
umläute

Reputation: 31284

the problem is that your Operating System(!) generates repeated key events if you keep pressing the key.

solution

so the simple solution is to tell your OS to suppress repeated key events.

workaround

the more complicated workaround is to keep track of the current state of the given key and suppress duplicate keydowns. this is most easily done if you only track a single key (rather than all at once):

e.g. an abstraction [keypress 97] that will detect keypresses of a (ascii 97):

[key]        [keyup]
|            |
[select $1]  [select $1]
|            |
[t b b]      |
|     [stop( |
|     |      |
|     +----- |
|           \|
|            [del 50]
|            |
[1(          [0(
|            |
| -----------+
|/
[change]
|
[outlet]

Upvotes: 2

jmunsch
jmunsch

Reputation: 24089

What about [keyname]:

Here is an example patch that will write to an array when multiple keys are pressed. It should be possible to use this as a polyphonic input. I think then using [tabread] and iterating the array index number would indicate whether a key is pressed or not (the index should match the ascii/key number):

#N canvas 800 301 544 205 10;
#X obj 23 23 keyname;
#X symbolatom 89 40 10 0 0 0 - - -;
#X floatatom 23 46 5 0 0 0 - - -;
#X obj 181 18 key;
#X floatatom 181 46 3 0 0 0 - - -;
#X floatatom 220 44 3 0 0 0 - - -;
#X obj 220 18 keyup;
#X obj 44 87 pack float symbol float float;
#X obj 67 117 print;
#X obj 46 151 tabwrite array1;
#N canvas 0 0 450 300 (subpatch) 0;
#X array array1 256 float 1;
#A 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
#X coords 0 1.2 255 0 256 100 1 0 0;
#X restore 277 33 graph;
#X connect 0 0 2 0;
#X connect 0 1 1 0;
#X connect 1 0 7 1;
#X connect 2 0 7 0;
#X connect 2 0 9 0;
#X connect 3 0 4 0;
#X connect 4 0 7 2;
#X connect 4 0 9 1;
#X connect 5 0 7 3;
#X connect 5 0 9 1;
#X connect 6 0 5 0;
#X connect 7 0 8 0;

Example with a + g pressed at the same time:

enter image description here

After pressing s:

after pressing 's'

While a:

while a

After pressing a:

after a

I was able to find something here as well: http://puredata.hurleur.com/sujet-3718-pdkb-basic-virtual-midi-keyboard

zipfile: http://puredata.hurleur.com/attachment.php?item=1635

Looks neat, not sure if it functions.

Upvotes: 1

Related Questions