Grab keyboard events

due_friday's icon

Hey everyone,

I am looking for a way to get the keys pressed in Max/MSP - but with a twist:

The thing is, I want to hit the up and down arrows to have things happen. This is all fine using [key] and [keyup], no problem.

The problem is, that other things that listen to these key events still react as well. If I have scroll bars, for example (actually, in particular), pressing the up and down keys will cause the window in focus to scroll up and down.

The question is: how can I grab the key events in such a way that they are ONLY read by me and not by anything else internally by Max. (Similarly how a [number] would prevent scrolling, or even a [key] object to read anything when the [number] gains focus).

Cheers!

AudioLemon's icon

What if you tried using one of the modifiers as well or some key combination. Not what you want but it might work.

due_friday's icon

Hm, seems like it still scrolls the window even with any modifier key pressed :(

...despite this also being a sub-optimal solution. There must be a way to prevent the key events reaching the window. Maybe with a [thispatcher]?

EDIT: nope, [thispatcher] also does not quite cut it. I could perhaps disable the grow attribute, catch my key event, and re-enable it. But, despite it being very ugly as the whole window flashes every time I do this, I also am not sure if I could disable the grow attribute fast enough for it not to scroll, all within a single key press.

If anyone has some more suggestions, please let me know! Cheers

due_friday's icon

Okay, I managed to hack this, but this solution only works in my specific case, in that I want to get the up and down arrow keys of the keyboard:

Get a [number] object and send it a "select" message when whenever you want to grab the key events. This way, only the [number] object selected will listen to the keyboard. When the user presses up or down, he will change the [number] object by one value up or down, also causing it to fire out its outlet. I then just calculate the difference to determine if the numbers increased or decreased and, hence, can determine if the user pressed the up or down keys.

Note that the number object has to be visible on lock for this to work. BUT, I managed to hide it using it inside a bpatcher and putting it outside of the viewport of the bpatcher (but effectively keeping it visible on lock).

Also, I think you cannot "deselect" it by sending it any messages. The user will have to click on an area manually to deselect it. Luckily, in my case, I just want it to be in focus when the user clicks into the area of said bpatcher and losse focus when clicking outside of it, so this also works like a charm for me.

Hope this might help someone...

seejayjames's icon

Nice workaround!

One thing:

"Also, I think you cannot “deselect” it by sending it any messages. The user will have to click on an area manually to deselect it."

Try a "select" message to [thispatcher] to deselect any object that has keyboard focus (number, flonum, textarea, jit.cellblock).

Mattheos's icon
Max Patch
Copy patch and select New From Clipboard in Max.

This is the DUE_FRIDAY idea with steroids :)

Two bangs are just to make window scrolling.

Dan Laüt's icon

Or use the active object:

Max Patch
Copy patch and select New From Clipboard in Max.