I am currently developing a media application in java using the gstreamer bindings for java. All is going well except for one small issue regarding the keyboard, specifically the media keys (e.g. play/pause, back, next).
I have Actions that work fine as buttons/menu items for these functions, and was hoping to allow the use of the keys that are on some keyboards (often on laptops). However, I could not find andthing in java.awt.event.KeyEvent to represent these keys. I tried running the snippet of code below, and it worked fine for most keys, including f1-12, but did not respond to 'fn' nor to the media keys while 'fn' was held.
Does anyone know how to get these keys working?
Code:
import javax.swing.*;
import java.awt.event.*;
public class Key extends JFrame{
public Key(){
JTextField f = new JTextField(50);
f.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
System.out.printf("%s : %d \n", e.getKeyChar(), e.getKeyCode());
}
});
setContentPane(f);
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
new Key();
}
}
If the e.getKeyChar() and e.getKeyCode() keys don't return anything when you press those keys, I'm not aware of any other way to get those events via the standard Java library, other than using JNI to get the codes at a lower level.
You seem to be on the right track, trying to get the key codes when a key is pressed, which is how you would discover key codes when you don't know what the proper constant is in Java.
Other than that, this question seems to offer some possibilities (though I'm not sure if this only works with Microsoft IntelliType devices, or if it will work cross-platform). When I use a Microsoft IntelliType keyboard on my Mac, for example, the media keys seem to work just fine.
Related
So, I have this method (with nothing in it yet)
class Transmitter {
public static void main(String[] args) {
}
}
and I want to put a keylistener in it, to detect when the space key is pressed.
I've looked at tutorials online, and they are all to complicated for my needs; I just want a simple "Print something in the console" when the space key is pressed. Thanks!
As some others have commented, there is no concept of a key listener from a console application. If you just want to read input keys from the console, you can use System.in.read() within a loop: What is the use of System.in.read()?
Not sure if that's what you are looking for.
I am trying to write a program that uses key events to activate a method. The code works on Windows machines but when transered to Mac it does no respond to my "Spacebar" being pressed. I presume this is to do with the different key codes used.
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
System.out.println("SPACEBAR");
grid.stepGame();
}
}
Any ideas how i can get this working on Mac?
Edit - The problem has been solved using the answer below - For note though it seems that on Mac the Frame never automatically regains focus, hence why the keylistener doesn't work is another JComponent is activated.
I'm uncertain as to your particular issue but it's a good bet that if you switch to using key bindings instead of key listeners your issue would disapear.
From the Java Tutorials site:
Note:
To define special reactions to particular keys, use key bindings instead of a key listener.
As an example
// Component that you want listening to your key
JComponent component = ...;
component.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0),
"actionMapKey");
component.getActionMap().put("actionMapKey",
someAction);
I'm currently trying to create a little remote-app for Android to control a MediaPlayer (like Rythmbox) on my PC.
Most media-players understand the special keys on my keyboard (like "play/pause" or "next/previous"). My idea is that the Android App sends a command (like "pause") to the PC. On the PC runs a normal Java-Application which receives this commands and simulates a key-press of this special button.
The advantage would be that you can use this App on all platforms for every player which supports this special keys (and they are on almost every new USB-Keyboard).
I searched the JavaDocs for a constant in the KeyEvent-class, but I can't find any. Does anyone know how to simulate a press of one of those buttons and if this is even possible with Java?
Additional library's are okay with me, too, as long as there is no other solution.
Also, I know i should use a Robot to simulate the key-press and this works for all normal keys on my keyboard. I simply can't find any way to simulate a key press on those special keys.
So, I think it's not possible to do this with pure Java. I tried something else to find out which key-code the special keys have, but this small program only returns 0 for those keys (it works for "normal" keys):
public class GetKeycode implements KeyListener{
private JFrame f;
private JLabel feld;
public GetKeycode(){
f = new JFrame("GetKeycode");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addKeyListener(this);
feld = new JLabel();
f.add(feld);
f.pack();
f.setVisible(true);
}
#Override
public void keyReleased(KeyEvent e) {
feld.setText(e.getKeyCode()+"");
}
public static void main(String[] args) {
new GetKeycode();
}
// Unused:
#Override public void keyPressed(KeyEvent e) {}
#Override public void keyTyped(KeyEvent arg0) {}
}
I hope this will be implemented in future versions of the JRE. But at the moment, there seems to be no solution for this.
Thanks for all the answers anyways.
Have you already tried to send the OS dependent key codes to the Robot? The multimedia keys are unfortunately not directly supported in Java yet, not even in Java 1.7 but most of the keycode definitions in java.awt.event.KeyCode have the same value as their native Windows pendants. The Robot doesn't filter unknown key codes directly in Java but lets its native back end decide what to do with them. So there is a chance that it might work at least on certain platforms.
The MUTE key code would be 0xAD. Here is a list of the Windows Key Codes.
VK_MEDIA_PLAY_PAUSE
VK_VOLUME_MUTE
VK_VOLUME_DOWN
VK_MEDIA_NEXT_TRACK
VK_MEDIA_PREV_TRACK
Control a Windows apps using Java
To temporarily solve your problem just google "rhythmbox android remote." There are some great projects already.
I am interacting with a third party application using it's API, and would like to move it to the front of focus (so that it is on top of all other open windows) when a user performs a certain action. While I can move my application up and down in the hierarchy pretty easily, there doesn't appear to be a way to interact with other windows. Is it possible to move another program's window to front with Java?
You can't do it in pure Java code, but you could using JNI. See In Java Swing how do you get a Win32 window handle (hwnd) reference to a window? for how to get a handle to the window. Then you could do something like http://msdn.microsoft.com/en-us/library/ms633545 to move it to the front.
Caveat: This is for windows only
It's not possible in pure Java and on certain OSes it's not even possible to cleanly get the other windows position (for example good luck doing that under OS X 10.4: OS X 10.4 does not have any documented mean to registered for other windows' events... There are "hacks", but they're really hackish, like requiring the user to turn on the "Allow Assistive Device" preferences, which requires the OS X admin passord).
I was looking for a solution to a very similar problem.
Until now I have found one - very fragile! - solution.
It works for my case, because my case involves running entire sessions using java.awt.Robot without user interaction, so it may - or may not - work for your case.
The solution is using java.awt.Robot to send the key strokes like Alt+Tab to bring the desired Window to the front.
This is fragile of course, due to multiple reasons:
The Robot cannot know how often Alt+Tab needs to be send to get the desired window to front. The window is one in many. This solution only works if it's already known which window in terms of Alt-Tab-count it is.
Depending on the OS, the required keystrokes might actually be something else.
In case the OS and the window sequence are known, i.e. the program can know upfront how many Alt-Tab keystrokes would be required, this could sometimes be a solution.
The following Java program demonstrates how to do this. If called without arguments, it generates exactly one Alt+Tab keystroke. If called with arguments, the program assumes the first argument is a number and it will generate as many Alt+Tab keystrokes as specified by that number.
You may want to play around a bit with the timing values given in robot.delay() and robot.setAutoDelay() in order to deliver the best experience on your machine. Hint: at least on Linux, robot.setAutoDelay() should certainly be less than the keyboard repeat delay, otherwise the OS would generate multiple Alt-Tab keystrokes in the system's event queue because of key repetition.
import java.awt.AWTException;
import java.awt.Robot;
import static java.awt.event.KeyEvent.VK_ALT;
import static java.awt.event.KeyEvent.VK_TAB;
import static java.lang.Integer.parseInt;
public class WindowSwitcher {
public static void main(final String... args) throws AWTException {
final int repeat = args.length != 0 ? parseInt(args[0]) : 1;
final Robot robot = createRobot();
robot.keyPress(VK_ALT);
for (int i = 0; i < repeat; i++) {
robot.keyPress(VK_TAB);
robot.keyRelease(VK_TAB);
robot.delay(500);
}
robot.keyRelease(VK_ALT);
}
public static Robot createRobot() throws AWTException {
final Robot robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(10);
return robot;
}
}
I am making a program that allows user to custom keyboard shortcuts, for this i need the available keys to be displayed, what is the best way to achieve this in java swing?
KeyEvent.class.getDeclaredFields()
I am intrested in dynamic example of below,
keysLST.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CTRL", "ALT", "SHIFT" }));
one way i know is to get all from the keyevent class but i am not sure how to integrate it to the list. any help would be appreciated.
Thanks
Instead of picking from a list, add a KeyListener to any component (a JTextField works) and record the key code as the user presses the key. You can also record modifiers (ctrl,alt,shift) this way.
public void keyPressed(KeyEvent e)
{
int keyTheUserJustPressed = e.getKeyCode();
// then use for ctrl/alt/shift
e.getModifiersEx();
// or use
e.isAltDown();
e.isShiftDown();
}