views:

340

answers:

2

My app is cross-platform, hence why SWT is being used. I can easily monitor for the Mac Command/Flower key press when the app is running using SWT (they have SWT.COMMAND as a key press).

I want to know how to see if the command key is being pressed and held down as the app opens. I found a few examples on here, but those were for caps lock, I need the command key. Any suggestions in Java or SWT code? Thanks.

A: 

I don't know the exact constants for the command key but I can help you with the design:

you want to see if a key is held before any app code is executed (or while). I'd consider using one of the two below methods:

1) have your main method call a method that sees if the key is pressed down (or embed right into it some code that checks)

or the second one, which I think is better: 2) upon startup, create a separate thread that checks (in tight sleep loop) that checks to see if the key is pressed down. If it is, use some form of inter-process communication (shared data structure, object notification, etc) to inform the main thread that the key was pressed down and invoke code you would want to when the key is pressed down.

Hope this helped.

vinnybad
A: 

It does not seem to be possible to detect this through Java or SWT. My work around was to have a splash screen open as the app loads. This way I can have the user click any key to continue, and when they do, I chack for the COMMAND and SHIFT keys. If either is pressed they cannot proceed.

KKlucznik