I need to create a program which fire event on every key press(like key logger). I couldn't able to get key value. This program will run behind and on every key press it fire event. I didn't find any thing related to this on net.
It is very strange that you cannot find anything.
You should user KeyListener to log key events. If only one event (e.g. keyUP, keyDown or keyPress) is relevant use KeyAdapter instead.
The problem is that key listener can be attached to any java screen element and catches events dispatched to this element only. For example you can create Frame and add key listener to it and capture all key events withing this frame.
You cannot capture key events outside the java application.
If you still want to do this you can use the following work-around. Create transparent window and add listener to it. The listener should catch events, log them and then imitate them on the same location on screen using java.awt.Robot.
This reference will help you to create transparent window: http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html
Other way is to use JNI/JNA directly or indirectly. For example xdotools can help you.
Related
I'd like to update GUI elements (JComboBox, JLabel, etc.) from code which shouldn't trigger change event. Is it possible to find out from java.awt.event.ActionEvent or java.awt.event.ItemEvent if the change was caused by an user or by running code like this?
combo.setSelectedItem("my item")
The answer is: no.
But in some cases you can try to analyze the current InputEvent. To get it, use EventQueue.getCurrentEvent(). For example if user has triggered the change on clicking of another component, you can compare the component of the input event and the component of the action event (OK I know: it's unsafe. But in some cases it can help to avoid incrementing of application complexity).
For a button you can get the event modifiers:
int buttonModifiers = evt.getModifiers();
If the button event was generated with a call to doClick() the modifier is 0, otherwise not.
By the way, you can find out such differences relatively easy by logging / printing using evt.toString()
.
I need that all the key events (including Carriage Return, TAB, etc) generated by all child or grandchildren components of a JFrame, to be listened for a single method inside this JFrame. I have googled a lot, but I have not been able to find the solution I need. I found a partial solution using "InputMap/ActionMap", but it only allows to add particular KeyStrokes on the InputMap, but I need all possible Key Strokes to be forwarded to the parent JFrame.
Thanks.
(I saw this thread, but I was expecting a solution within the Swing API, specifically to address this subject.
Check out Global Event Listeners which gives you a couple of choices:
Use an AWTEventListener.
Us a KeyEventPostProcessor.
I've got a KeyboardFocusManager with overridden dispatchKeyEvent() method to handle navigation for arrow key and enter. I need to handle key events for as long as the key is pressed. Unfortunately, holding down enter results in KEY_TYPED events which do not contain the key location. Is there a way to find out which enter is currently being pressed? Or can I suppress KEY_TYPED events for enter in favour of KEY_PRESSED events?
edit:
I can't tell you the reason why it's done with a KeyboardFocusManager instead of KeyListeners but I'm sure there were reasons it was done this way. As of now, it is not possible anymore to change this. The problem is, that for our system(an old terminal emulator), the left enter key is a navigational key. Pressing enter moves the focus to the next textfield after the current line. The right enter key is a command key that sends the user input to the server.
Using the KeyEventDemo found in How to Write a Key Listener, I am unable to reproduce the effect you describe for either KEY_PRESSED or KEY_RELEASED. Of course, "KEY_TYPED events do not have a keyLocation."
It's not clear why you are using a Focus Listener, as many components already bind certain keys to a defined Action. The example binds the arrow keys, which respond to auto-repeat events regulated by the host's preferences.
I'm working on a map editor for my college project. And I had a problem that the map panel is not listening key event while it should.
This happens when I add a ToolBarPane (which extends JPanel) with JComponent such as JButton, JComboBox that implements ActionListener on it AND the map panel (which extends the JPanel) together on to the Frame (I used BorderLayout). I have System.out.println statement to test if the key press is received, and it's not printing, if I remove the ToolBar, the key listener works again, so is the mouseListenner is disabled just like the keyListener, which means I can't handle press events etc, but the mouseListener works fine and I can still handle mouse move event.
Here is a screen shot how it works without the ToolBarPane
http://img684.imageshack.us/img684/3232/sampleku.png
note that you can use the mouse to put images on the map, you can also select images using the mouse just like a laser tool, and by pressing number key you can switch between different images, this works fine until I add the ToolBarPane which shows like this:
img291.imageshack.us/img291/8020/failve.png
(please add http before that, I can only post one hyperlink)
(I can't post images here because I'm a new user)
With the ToolBarPane on I was no longer able to handle the key event.
I guess it might by that the focus as been transferred to that panel somehow, but not sure at all.
Does and body know this and can help me out?
Thanks very much
I suggest you use the InputMap and WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or something similar. Excerpt from How to Use Key Bindings:
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
The component contains (or is) the component that has the focus. This input map is commonly used for a composite component
That has worked very robustly for me. Have a look at my other post for more information and actual code examples:
Keyboard input for a game in Java
or this tutorial:
Swing: Understanding Input/Action Maps
You should NOT be using a KeyListener.
Swing was designed to use Key Bindings which is far more flexible. Check out my quick summary of Key Bindings which also includes a link to the Swing tutorial which conains far more detail.
(I can't post images here because I'm a new user)
An image doesn't help much anyway. If you need more help post your SSCCE which shows the problem (after trying the above suggestion).
I've a problem with setAccelerator(). Right now, I have the code that works for Ctrl+X for DELETE operation. I want to set the accelerator to Shift+Delete as well for same JMenuItem.
My code as follows:
JMenuItem item = new JMenuItem(menuText);
item.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_X, KeyEvent.CTRL_MASK));
item.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_DELETE, KeyEvent.SHIFT_MASK));
but this is working only for Shift+Delete operation. Seems it is overriding the Ctrl+X operation. Can we make both these keystrokes work at the same time?
Please guide.
Yes it can be done. Behind the scenes the setAccelerator() is just creating a Key Binding, however as you noticed the second binding replaces the first.
So, you need to create an Action (not an ActionListener) to add the to the menu item. Read the section from the Swing tutorial on How to Use Actions for more information. Now that you have created the Action, you can share the Action with another KeyStroke by manually creating a Key Binding. You can read the section from the Swing tutorial on How to Use Key Bindings for a detailed explanation. Or you can read my blog on Key Bindings which give some simple code examples.
This second binding will not show up on the menu item itself.
From: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/AWTEvent.html
The masks are also used to specify to which types of events an AWTEventListener should listen.
So you can combine the mask for two keys, but not the KeyEvents.
item.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_X, KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK));
A workaround solution would be to catch the KeyEvent in the middle (after your component fired it, but before your listeners will act on it) and check, whether its one of the two combinations. Then fire one event, on which you programmatically agree to represent the action you wanted.
The second call indeed overrides the accelerator. If the method starts with set, there will be only one. If the method starts with add, you can have more than one (for example for a number of listeners).
If you want multiple keystrokes to do the same, I think you should add a keyListener to the top frame (or panel, dialog, ...) which invokes the action listeners added to the menuItem.