Can I click "Windows" key in Java? I need to emulate pressing Win + ARROW_UP keys. May be this button has Key Unicode and it could be called by .getKeyFromUnicode() method? Will be appreciated for any help. Thank you.
private void pressKey() {
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_WINDOWS)
r.keyPress(KeyEvent.VK_UP); //Windows button is still pressed at this moment
r.keyRelease(KeyEvent.VK_UP);
r.keyRelease(KeyEvent.VK_WINDOWS);
} catch (Exception e) {
e.printStackTrace();
}
}
If you want to simulate pressing and holding one button, while then typing another, use type(TheKeyDoingTheAction, KeyModifier.TheKeyYoureHoldingDown It's written like this:
type(Key.UP, KeyModifier.WIN)
Please check docs and samples here: http://doc.sikuli.org/keys.html
Runtime.getRuntime().exec("rundll32 user32.dll,LockWorkStation");
Note: This will work for Win OS only.
Try out the Robot class to emulate a key press. Use the Key Event class's VK_WINDOWS constant to press the windows key (this can be generalized to also press the up key with VK_UP):
import java.awt.Robot;
import java.awt.event.KeyEvent;
Robot r = new Robot();
r.keyPress(KeyEvent.VK_WINDOWS);
r.keyRelease(KeyEvent.VK_WINDOWS);
Keep in mind the Robot class throws an IllegalArgumentException if the key is invalid. I'm not 100% on what happens if you try to press the Windows key on a non-Windows OS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With