Output keystroke?

George Luther's icon

I'm trying to control the arrow key in Ableton's piano roll with an M4L device for enhanced 'step recording'. Is there an inherent way to do this with M4L? If not is there a natural way to output keystrokes with max or js or something? Also any info about simulating a HID drivers would be useful for future projects RE: joystick, keyboard, mouse, and trackpad enhancement.

p.s. aka.keyboard is not an option as I'm on windows

Source Audio's icon

11strokes2 external

http://www.11olsen.de/code/max-objects/11strokes

George Luther's icon

Thats super useful, thanks alot! Do you know how it does what it does? Can't seem to see the source code cause it's an mxo, but I'd love to know how the code actually does the keystrokes.

Source Audio's icon

read the help file
Also goggle for keycodes

Source Audio's icon

You can try shell - osascript to execute systemevents key codes if you really have to make own version.
There was also java version mxj_autobot, I dislike use of java though.
https://sites.google.com/site/theoldmanthesea/download
but 11strokes is rock solid and works on all platforms.

2-xite's icon

Hello to Max-friends,

I like to use 11strokes, but I really miss the specific number-codes (the keypads 0-9, -, +, /, dot and the Enter) on the keypads-side. These key-numbers are different from the similar ones on the main keyboard layout and there is no substitution for them.

Would you please share an advise if it possible to add those key-strokes to the available list?
If not, what would you do then?

I appreciate your assistance!


Alfredo

Source Audio's icon

If you are mac user you can send keycodes using systemevents.
example :
tell application "System Events"
key code 69
end tell
----------------
in Max insert this message between key code number and shell :
osascript -e 'tell application \"System Events\"' -e 'key code $1' -e 'end tell'

  • Numpad 1: key code 83

  • Numpad 2: key code 84

  • Numpad 3: key code 85

  • Numpad 4: key code 86

  • Numpad 5: key code 87

  • Numpad 6: key code 88

  • Numpad 7: key code 89

  • Numpad 8: key code 91

  • Numpad 9: key code 92

  • Numpad 0: key code 82

  • Numpad *: key code 67

  • Numpad /: key code 75

  • Numpad +: key code 69

  • Numpad -: key code 78

  • Numpad =: key code 81

  • Numpad .: key code 65

  • Numpad clear: key code 71

Source Audio's icon

P.S. on windows there is this little utility

http://cpap.com.br/orlando/SendKeysMore.asp

2-xite's icon

Hello to SA,

Thank you for the quick and very useful replay. Yes, I'm on Mac (High Sierra) and did tested and it work as expected!

I still have a question. It seems, that it would be not a good idea to combine 11strokes and the osascipt for key-combinations like: "command" + "plus" (on the keypad side)?
So I have to understand how to press a modifier (like command), hold it and release after another key code through the osascript? I understand that it should be in "one" string like:

osascript -e 'tell application \"System Events\"' -e 'key downcommand' -e 'key code 69' -e 'key upcommand' -e 'end tell'

But the string above does not work...

Thank you in advance!

Alfredo

2-xite's icon

PS
I think that I will have to replace the 11strokes completely with osascript if I wish to use the complete keyboard layout and not trying to mix both in a patcher.
The ordinary keys numbers could be extracted from the [key] object, but how can I find out what the key numbers of keys like F1, F2... + Fn etc.? The [hi] object gives me much different numbers, but those seem not to be useful for osascript...

Source Audio's icon

here is a good documentation :

https://eastmanreference.com/complete-list-of-applescript-key-codes

only that osascript and shell in Max need a bit different syntax, like this:

osascript -e 'tell application \"System Events\"' -e 'key code 69 using command down' -e 'end tell'
it is not necessary to "release" the modifier keys

2-xite's icon

Just wonderful, it work as intended!

Thank you so-much for all info, SA!

With compliments,

Alfredo

2-xite's icon

Hello Max-friends,

I'm sorry to come back with this very simple question, but I can not figure out how to use multiple modifiers within one single message, for example if I wish to use a key-combination like this: Cmd+Shift+z

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

This source from the link below is great and it seems that I did as it required, but it still does not work... What did I miss?

https://eastmanreference.com/complete-list-of-applescript-key-codes

Thank you for the assistance in advance!

Source Audio's icon

osascript -e 'tell application \"System Events\"' -e 'key code 6 using {command down\, shift down}' -e 'end tell'

max is not a text editor and will allways play games with you
when it comes to text and messages...
comma in max message splits and iterates message contents.
to keep it a comma one needs to escape it using backslash or several ones
depending on situation.

2-xite's icon

Bravo, SA!
It really works now, also other variants with 2, 3, 4 key-mods all together...
Oh, I do remember that the backslash (or two) is needed a.o. in [regexp] if we wish to treat a metacharacter literally.
Again, with compliments!

2-xite's icon

Its me again... :)

I'm trying to build a basic key-combinations-recorder and I wish to use the osascript as its base, again thanks to Source Audio for his valued assistance.

Below is a basic idea, but I think its not very elegant in the way how I'm trying to collect (or add) the 4 key-mods. It works, but maybe you can give me a hand and tell how more efficient to collect the modifiers?

The issue is, indeed, that the [message] formats the content (with some special characters) in its strange way and I'm trying to compensate it with some extra characters.
Another thing is, that the very last key-modifier (in the 'collection') may not have any character after, otherwise a syntax error will occur and the command-line will not work....

In any case, would you please take a look at the patcher? I'm not lazy and I'm trying to do as much as possible by myself, but after many hours of tweaking I think its better to ask the pro's and this way I will learn more than only keep on trying...

Thank you in advance!

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

Source Audio's icon

why don't you simply add last or single modifier
without adding comma ?

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

Source Audio's icon

And maybe simplify it a bit like this

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

would be easier to make more key combinations using same modifiers

2-xite's icon

A big applause, Audio Source!

Just really cool.
I'm a fan of the second solution with [sprintf] too, which gives 'recycling' possibility of the previous key-mods.

In which way would you advice me to go if I would go a step further with the idea of adding more 'ordinary' keys to the key-actions. Let me start with just an example of the message:

"7 1,55 1,7 0,3 1,3 0,69 1,69 0,55 1,55 0,65 1,65 0,54 1,54 0,3 1,3 0,66 1,66 0,61 1,61 0,3 1,3 0,58 1,58 0,51 1,51 0,47 1,47 0,64 1,64 0,60 1,60 0,3 1,3 0,59 1,59 0,61 1,61 0,64 1,64 0,51 1,51 0,7 1,74 1,74 0,7 0,1 1,1 0,"

If I put this message in 11strokes I get a sentence: I wish to learn more!
As you see, Shift-key is twice used.

Because of the complex formatting in osascript it will not so easy to add more ordinary keys in one command line. I believe that it would be few series of messages.

So, would you advice me to collect separate messages (with various key-combs) in a [coll] object and after it firing-up the stored only preset-numbers in order to recall the complete sentence or still trying to add more and more keys in one message, or maybe something else?

Off topic:
I wish to use this key-recorder for automation in my music-score editing program which has insane much repetitive tasks and with those commands it would be much more pleasure to work.

Before of this I worked with aka.keyboard, but I really must upgrade my system to 64 bit and aka.keyboard stopped to work. So I tried the 11.strokes, but this one misses the keypads-support as you already know and this pushed me here...
But the osascript is really cool and I like to use it ;-)

Source Audio's icon

try this :
osascript -e 'tell application \"System Events\"' -e 'keystroke \"I wish to learn more!\"' -e 'end tell'
or this :
osascript -e 'tell application \"System Events\"' -e 'keystroke \"I wish to have sun\, fun\, and more... !\"' -e 'end tell'
One would think the actual text could be sent as a symbol, not taking care about comma ...
but it is max, not apple script.
------
One can at least separate that ever repeating sequence using :
prepend osascript -e 'tell application \"System Events\"' -e
and
append -e 'end tell'
to
shell
and just store and send actual key codes or strokes part to it.

2-xite's icon

PS
My excuses, but I forgot to ask: is that the 'official' way below to strike and hold a mod-key via the osascript?
osascript -e 'tell application \"System Events\"' -e 'key down command' -e 'end tell'
and to release:
osascript -e 'tell application \"System Events\"' -e 'key up command' -e 'end tell'

if so, how to do it with an explicit key code, like key code 60 (R. Shift)?

Source Audio's icon

tell application "System Events"
    key down {command}
    keystroke "c"
    key up {command}
end tell

this is "official" way, or at least from what I remember.
key up and key down commands only work with modifier keys
but actually execute for ordinary keys as well :
repeat 2 times
    tell application "System Events"
        key down {shift}
        key down "w"
        key up "w"
        key down "s"
        key up "s"
        key up {shift}
        key down "w"
        key up "w"
        key down "s"
        key up "s"        
    end tell
end repeat
I don't know if using key up for "w" & "s"
is making any difference.
keystroke is allways key up & down, that's why it is called stroke
like
tell application "System Events"
    keystroke return & "bcd"
end tell

2-xite's icon

Just missed your reply, while typing my next questions.. Thank you again!

Really wonderful, SA. You are so attentive.

Yes, I even did not think to keep all the prepend/append-rituals out of the [coll] and keep its presets 'clean' (and easy to repair/change directly in the .txt of the [coll] ).

2-xite's icon

the last one:
how to explicit tell the script to hit/keep R. Option (key code 61) down (and separately release)?

(I did try many variants already, nothing works here)

Source Audio's icon

I don't know of separate "right" modifier keys when it comes to key down and key up.

2-xite's icon

Yes, it's sinister for me as well... And I do not much care about this much, but some progs make a difference in these R/L key-mods...
But anyway, your assistance is incredible!
I hope the other Max-users will value all this as much as me.

With true compliments,

Alfredo

2-xite's icon

Almost off-topic, but still about the osascript.

SA, do you know how to click and hold mouse buttons (for dragging) with this super script?
Man, the mouse does not click and hold with the 11clicks...
I hope there is a solution for dragging-programming...

Source Audio's icon

doing a google search would have given you same result in few seconds:
nothing but click is possible by system events.
and

https://www.bluem.net/en/projects/cliclick/

which is newer than
http://www.hamsoftengineering.com/codeSharing/MouseTools/MouseTools.html

----------
I guess you tried mxj_autobot for keys and mouse execution ?
download link in one of earlier posts here.

2-xite's icon

Thank you, SA.
Indeed, I tested Autobot before my question here, but also... no love...
Hmm, It feels not safe to install more unknown and untested scripts.

Also did tested the MouseKeys from Accessibility (to record keypads as mouse incl. click-hold event with the 0-keypad), but it seems now that I can not to grab those inside Max.
I have to think more...

Source Audio's icon

There is nothing wrong with mxj autobot.
It works on HighSierra and would do most of the things you need.
I dislike to use java simply because of mac - win differences,
and system versions etc but for own usage, it is just fine.
autobot can move mouse to screen coordinates, press it, move somewhere else and release it.
So why not give it a try...
I would personaly not automate mouse clicks, drags etc, one could have wrong app in front.

2-xite's icon

I'm sorry not to be very clear, but I did tested mxj_Autobot already and it did not worked; at least it did not what I've expected (like click/hold and change the mouse coordinates in Max in order to move a desired window, as-like Aka.mouse did under 32bit).

Just now I've tested with the calculator app and found that the clicked/hold button is really staying pressed (I see the visual feedback of the calc. buttons).

It means that at the moment when I try to use 11strokes or autobot (similar as I did with aka.) the system does not redraw the position of the (dragged) moving window, only the mouse pointer. When I click again during that movement, the window, that I try to move through Max, "jumps" in the new position.

Maybe I must repeatedly 'tell' the system where is the 'manipulated' mouse position during this moving action without of physical clicking on it and then it will redraw the moving window smoothly as it does when I do it in real.

What do you think about all this?

Source Audio's icon

I don't know, why you can't make something work without the code used.

Automove.mov
video/quicktime 178.60 KB


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

2-xite's icon

Thank you for all this attachment and your patience, SA.
Unfortunately the [autobot] here does not move the (resized) window as it does on your video, only the mouse pointer! Really.
Can you believe this?

After tweaking I discovered by accident, that if I put the window in full size (option+click on the green button on up-right-side of the window), it does move some windows of Max or Text editor, but not Safari or Calc.app.
I'm just on High-Sierra... It should work normally...

2-xite's icon

Does it need some extra permissions, like this?
https://support.apple.com/guide/mac-help/control-apps-mac-mchl07817563/mac
If so, I don't even see "automation" at "Security & Privacy" on my current version OS...
Very strange that it works on your system.

11OLSEN's icon

Hi, I updated 11strokes to support the keypad keys
http://www.11olsen.de/code/max-objects/11strokes
and I pushed the source code to a github repo
https://github.com/11ols/11strokes

2-xite's icon

Oh, its really great!
I go to test it now.

Thank you very much, 11Olsen!

Source Audio's icon

Thank you OLSEN, your tools are very usefull to many max users.

2-XITE - I can't say why it does not work on your system.
I allways get rid of any apple restrictions no matter what system,
so can't help you about that, but mouse click is just a mouse click,
either it gets executed or not.
It maybe has just to do with time needed
to switch between Max and the app which gets clicked ?
Increasing delay times could test that.

2-xite's icon

After 'some' time of testing, I've discovered, that the X-values above the 340 pixels will do the trick, everything below will not be rendered on my old system.
I've started to think, does it have to do with the sticky edges of the OSX? And if so, than I will turn temporary off by using Option during 'dragging'. And, indeed, the trick started to work!

So, If I wish to use smaller horizontal values (below 340pix) to displace my window, I must use Option down to achieve that movement. The only thing, is that the Max-window disappear from the desktop and I need to find a way to recall it quick back...

At last, but not least: the 11strokes v. 2020 is fantastic, it covers all keyboard layout now and on my system it works quicker than osascript in cases when I need to use many separate characters in short time (for key-commands like zoom in/out, go to next/prev. frame, little nudges of objects etc.).

Thank you for all support!

Source Audio's icon

I disabled any mouse or trackpad gimmicks on my mac, any automatic poping windows, edges, corners or whatever else. maybe that's why automating mouse works.
option - drag is a bit dangerous, if on does that in finder, it copies stuff

2-xite's icon

Thank you, SA. I will turn all that flair off too, it will probably make my desktop working smoother. Indeed, Option + drag is in any other app dangerous, because it clones the selected stuff...

11OLSEN's icon

@2-XITE I noticed the same problems with simulating click n' drag to move a window using Windows 10 and 11clicks. It's only true for some windows for example the Live app window. I think any other drag operation (not trying to move a window) can be simulated.
And when i mix-in a real mouse movement, suddenly the simulated movement is moving the window as expected. Not yet sure why..

2-xite's icon

Excuses for the delay, my patch should running by tomorrow, so I will test it on W10 too and will let you know...

Jazz's icon

Hello Olsen, your 11 strokes object is really great!
But I just changed my mac, my old one was from 2008 and it's dead! I have a new one that has an m1 chip and it no longer works!
Do you have any idea why?
Thanks again !