Simulate a key held down in Java

Thread.sleep() stops the current thread (the thread that is holding down the key) from executing. If you want it to hold the key down for a given amount of time, maybe you should run it in a parallel Thread. Here is a suggestion that will get around the Thread.sleep() issue (uses the command pattern so … Read more

Fire Form KeyPress event

You need to override the ProcessCmdKey method for your form. That’s the only way you’re going to be notified of key events that occur when child controls have the keyboard focus. Sample code: protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { // look for the expected key if (keyData == Keys.A) { // take … Read more

How to make browser full screen using F11 key event through JavaScript [duplicate]

This is now possible in the latest versions of Chrome, Firefox and IE(11). Following the pointers by Zuul on this thread, I edited his code to include IE11 and the option to full screen any element of choice on your page. JS: function toggleFullScreen(elem) { // ## The below if statement seems to work better … Read more

How can I perfectly simulate KeyEvents?

There is no “easy” way to translate a virtual key to a actual key sequence or back again, at least not that I’ve been able to find. The two main ways of dispatching key events is either via java.awt.Robot or directly through the system event queue. Which you want to use will depend on what … Read more

Android: Listen for power key press

Although I couldn’t capture the hardware key I ended up being able guess that the power key was pressed by using a broadcast receiver that listens for whether the screen is turned off or on. I used: if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { //do something } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { //do something else } I was able to … Read more

emulate media key press on Mac

That took some time and many hacks (trying around with ctypes, the IOKit native interface, Quartz and/or Cocoa). This seems like an easy solution now: #!/usr/bin/python import Quartz # NSEvent.h NSSystemDefined = 14 # hidsystem/ev_keymap.h NX_KEYTYPE_SOUND_UP = 0 NX_KEYTYPE_SOUND_DOWN = 1 NX_KEYTYPE_PLAY = 16 NX_KEYTYPE_NEXT = 17 NX_KEYTYPE_PREVIOUS = 18 NX_KEYTYPE_FAST = 19 NX_KEYTYPE_REWIND = … Read more

How do I capture Keys.F1 regardless of the focused control on a form?

Yes, indeed there is. The correct way for the form to handle key events regardless of the control that currently has the input focus is to override the ProcessCmdKey method of your form class: protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.F1) { MessageBox.Show(“You pressed the F1 key”); return true; … Read more

jquery how to catch enter key and change event to tab

Here is a solution : $(‘input’).on(“keypress”, function(e) { /* ENTER PRESSED*/ if (e.keyCode == 13) { /* FOCUS ELEMENT */ var inputs = $(this).parents(“form”).eq(0).find(“:input”); var idx = inputs.index(this); if (idx == inputs.length – 1) { inputs[0].select() } else { inputs[idx + 1].focus(); // handles submit buttons inputs[idx + 1].select(); } return false; } });

How can a KeyListener detect key combinations (e.g., ALT + 1 + 1)

You should not use KeyListener for this type of interaction. Instead use key bindings, which you can read about in the Java Tutorial. Then you can use the InputEvent mask to represent when the various modifier keys are depresed. For example: // Component that you want listening to your key JComponent component = …; component.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, … Read more