Getting Keyboard Keys when Application is not frontmost

Is it possible to get the keyboard keys pressed, when the application is not frontmost?
Or is it possible to “send” a string same as keyboard input from an app not frontmost?

Platform Mac OS X, Desktop (no plugins).

Use Keyboard.KeyDown and Keyboard.KeyUp

A simple 20 millisecond multiple timer suffice to get all punched keys anywhere in the system.

You can send keys from AppleScript, but chances are that will never fly by the Mac App Store, because it needs system events which are completely taboo.

BTW plugins to send keys will not work either in a sandboxed application. I tested it for my Char Menu app.

Thank you Michel for the fast answer.
I more or less already used the technique you advised but…
I had to change to Async Keys.

The AppleScript way is ok, this app will not go to the App Store.
Unfortunatly anything is still not ok.
Some text gets lost in the AppleScript part.

That depends, are you making a keylogger or trying to register for a global shortcut?

What do you mean ?

No keylogger.
Just want to “forward” a scanned barcode of a barcodereader serial connected.
It seems to be very strange and related to the optional pressed keys (cmd, alt, ctrl, shift) while scanning
Have to check it out a bit more…

If the scanner is sending down keypresses instead of characters or strings then when the keypress from the scanner meets the modifier key from the keyboard, it gets turned into a shortcut rather than being a typed character.

I’ve actually seen this issue before, and lucky for that case they only needed numbers, so we just string replace the symbol with it’s number alternate (fixes the issue if someone’s holding Shift.)

Depending on what characters you’re expecting to scan, that may not work (you would never know that the char from the scanner was A if it met a cmd modifier, it would select all instead)

You can register for global keyboard shortcuts with system notifiers, that’s why I was asking; but I don’t think that’s going to work for your case since you’re looking for more than a single shortcut. I’m also not sure how it’d be done with declares in Xojo.

Yes I understand what you mean and I think that is what is going on.
So just reading and processing the code is not a problem.
I do different things (open window and showing related data, setting clipboard text to the barcode), related to different pressed keys.
I fear I have to change to use the “no key pressed” case to send the code further by AppleScript or create an extra code for that case.