Key Listening without a textfield or similar

Ok, I want to let the program call a method when a key is pressed, but so for I have only been able to do this when i added a KeyListener to an object like a textfield.
You can imagine this is a little bit annoying. I want something that listens to my Keys without some field. A panel was quite hard to implement and then I read something about ProcessKeyEvent and if you want this to work you have to do SOMETHING with enableEvent(), but I have no idea what. Can anybody tell me plz or give me another solution to this problem.

I think JComponents can listen for KeyEvents (as a previous post explains, there should at least be some component available to listen for the events)
The code below works for my JTree based component.
tree.addKeyListener( new HierarchyTreeKeyListener(this) );Where the listener class looks like...
     class HierarchyTreeKeyListener extends KeyAdapter{
          HierarchyTree tree;
          public HierarchyTreeKeyListener(CTree treeIn){
               tree = treeIn;
          public void keyTyped(KeyEvent kt){
               //get a handle on the selected node. This will be the root for the search
               DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
               //System.out.println("selected node: "+selectedNode);
               char key = Character.toLowerCase(kt.getKeyChar());
               boolean found = false;
                    DefaultMutableTreeNode queryNode = (DefaultMutableTreeNode)tree.charSearch(key, selectedNode);
                    if( queryNode != null ){
                         expandPath( queryNode );
          }//end of method
     }//end of class HierarchyTreeKeyListener

Similar Messages

  • Using a key listener without a container?

    Is it possible to use a keylistener that is always listening even if you don't have any keylistener containers open? I guess i would compare my need to a keylogger, although that is not what i am developing. Thank you for your help.
    -- Brady

    Let me guess--you want to detect any keystrokes on the computer, but you don't what to have an application that has to have focus, so you want to peek into the Windows message queue--right?
    Your answer will be: use JNI to do it at the native level--as I assume you desire, but don't want to say.

  • Getting key events without a jcomponent...

    Is it possible to get key events without adding a keylistener to a jpanel? I want a class of mine to manage input of it's own, but it has no reference to a jpanel. They don't necessarily have to be KeyEvents, but just some way to detect if a key has been pressed (like the arrow keys). How can I do this? Thanks.

    Lots of components can listen for key events.
    What does your class subclass?It doesn't subclass anything. I am creating a custom menu system for my game using images I have made for the tileset. I would like it to handle some keyboard events of its own. (Like if the down arrow is pressed, it will move the focus down to the next component on its own). Right now I am currently passing the key events from my fullscreen jpanel to the menu class and it is handling them that way. Thanks.

  • Air Native Extension - Java Key Listener

    Hello all,
    Firstly I'd like to thank you for creating an excellent and helpful community. I have an issue I have been stuck on for around 2 weeks now.
    I am creating an android native extension to listen to key input from a proprietary remote control device. GameInput does not recognise it as a game controller so we have to create a native extension to get the input. I have it working fully except for one issue; if the game loses context (eg the user gets a phone call or the game goes into the background for any other reason) I do not get any other input.
    The way I am doing it is to attach an onKeyListener to the currently focused View (an AirWindowSurfaceView). This works perfectly, but when the context is lost I'm assuming the AirWindowSurfaceView changes and I cannot find a way to get a reference to the new one. Here is my key listener setup code (from the native Java):
    public void updateListeners()
        if(_view != null)
          _view.setOnKeyListener(null); //remove from the old view
          _view.setOnGenericMotionListener(null);
        _context.dispatchStatusEventAsync(_view.toString(), "view"); //send the current view details
        _view = _context.getActivity().getCurrentFocus();  //set the new view
        _context.dispatchStatusEventAsync(_view.toString(), "view");
       if(_onKeyListener == null)
           _onKeyListener = new NativeGamepadKeyListener(_context, this); //create new key listener
       if(_onGenericMotionListener == null)
           _onGenericMotionListener = new NativeGamepadMotionListener(_context, this); //create a new motion listener
       _view.setOnKeyListener(_onKeyListener);          //set on the new view
       _view.setOnGenericMotionListener(_onGenericMotionListener);
    This updateListeners function is called when I get a focus change event on the current view (attached in a similar way) but this doesn't seem to keep it up to date with the current View.
    Please note I'm a newbie at making extensions like these and might be going about it totally the wrong way - if I am and you have any suggestions as to the best way to use an onKeyListener in a native extension I'd love to hear it.
    Thanks in advance for your help!

    I am not able to solve this yet. Is anybody else facing this this problem.

  • JTable TAB key listener

    Hi all:
    I'm trying to write a filterable jtable. User can input the filter rule in the table header and the table displays the filtered data at the same time when the user inputs letters. The table filter works fine. But when I try to add "TAB" key listener, I find that I can't track the "TAB" key. I use a customerized cell renderer to display the filterable table header, a customerized cell editor to edit the filterable table header. The cell editor is created like this:
    JTextField textField = new JTextField("");
    DefaultCellEditor cellEditor = new DefaultCellEditor(textField);
    And I add a KeyListener to the textField. The KeyListener can track other keys like "Shift", "Alt", "Ctrl", but it can't track "TAB". It's weird. I also add the KeyListener to JTableHeader, JTable, but when I edit something in the JTextField, I still can't catch the "TAB" key. Have any ideas? Thank you for your time.

    by default the Tab is used (and consumed) by
    focusManager to move focus between components. If you
    want to get it in a component you have to signal the
    manager to keep it's hands off. How to do so depends
    on the jdk version. Focus handling changed
    considerably between 1.3 and 1.4: Prior to 1.4 you
    have to subclass the component in question to return
    true on isManagingFocus(), since 1.4 you have to set
    the comp's focusTraversalKeys to make the manager take
    those instead of the default keys.
    Greetings
    JeanetteThanks Jeanette, it works. I use the Component.setFocusTraversalKeysEnabled(false) to disable all traversal keys of JTextField and add a KeyEventDispatcher to the KeyboardFocusManager. It finally works. Users can use "TAB" keyStroke and "Shift-TAB" keyStroke to traverse in the filterable table header now. Thank you for your great help and the $$ are yours.

  • SetFocus messes up key listener

    I have a textfield attached to a movieclip attached to a
    movieclip on the stage. I have an object set to listen for key
    events, and that object defines an onKeyDown function. If I run the
    movie and click on the textfield manually, a cursor appears in the
    textfield, and when I enter text the onKeyDown function executes.
    If instead I use Selection.setFocus(myTextfield), the cursor
    does not appear, but I can still type text in the textfield.
    However, this apparently makes a big difference, because now the
    onKeyDown function in the key-listener object does not execute. Is
    there a way to programmatically set focus and still have key
    listeners work? What is the difference between the
    Selection.setFocus kind of focus and the other kind of focus when a
    cursor appears?

    create an array. it's quick and easy if you work cleverly.
    create an empty array and push your ascii keypress codes into
    it. then test your project and keypress the numbers and letters in
    the order you want them to be listed in your array.
    when you're finished trace your array (in order), copy the
    output panel and in the authoring environment paste your ascii
    array.

  • Key listeners without a component?

    Hey guys,
    So I have an application with several sliders, buttons, etc. and I would like to set shortcuts for certain parts of it. For example, when the user presses the "n" key, one of the sliders should increase in value. I have looked into this, and found that key listeners only work on a component that has the keyboard focus. Does this mean I should give my main JPanel the keyboard focus and make the shortcuts change the sliders in that listener? What happens if the focus changes from the main panel to one of the buttons and the key is then pressed, will it still call the key listener on the JPanel?
    The best way would be if there was a listener that just listens to the keyboard and is called when a key is pressed, without any need for a specific component. Does anyone know of a way to do this? Thanks beforehand!
    Cheers,
    Lukas

    Young_one wrote:
    Well, the problem is I have 3 applets running on the same page, and so, they don't all have focus at the same time, but the key bindings need to work at any time, no matter which applet the user last clicked in. So I guess the question is if there is a way to create a listener that is not attached to a specific components focus. I would think that a listener that just listens to the keyboard doesn't have to specify a focus, but I guess it does? Thanks for all your help so far.Ah, I'm not an applet expert, but as far as I know, key binding and key listeners require that the app doing the listening have focus: with key listeners the component itself has to have focus, but with key binding, the component that is assigned the binding doesn't have to have focus, but at the very least the window that holds it does. I know of no way around this through standard Java.

  • LISTENING FOR BACKSPACE WITH A KEY LISTENER

    I have a key listener and I want to know how I find out if backspace has been pressed.
    Thanks

    I created a JTextField and added it to a JPanel which was added to the contentPane.
    When I add my key listener to a JTextField or the the JFrame, the code suggested above works fine. (JKD1.3, Windows 98)
    When I add the key listener to the JPanel it doesn't work.

  • How can I add the key listener to JFrame

    Hi,
    How can I add the Key Listener to the JFrame? I want to show the Windows default popup which comes after right clicking on the frame's header. I want to add the key board support for the same

    1. Make sure that key events are enabled on your component. (AWTMulticaster.enableEvents(...)
    2. add the keylistener

  • Tab and Key Listener

    I have put a key listener in a JTextField, but when I hit the tab key it does not generate a keyPressed event. Currently tab does nothing not even cycle through GUI components. Can you help me figure ouy how to get it to recognize the tab key.
    P.S. The code that is inside the tab block works as I tried it with other keys so that is not the error.

    I would search the forums for jtextfield and tab in the title
    http://search.java.sun.com/search/java/index.jsp?qp=&nh=10&qt=%2Btitle%3Ajtextfield+%2Btitle%3Atab&col=javaforums

  • "c" key types without being hit

    I have a MacBook Pro 17" late 2011 - the "c" key types without being hit. Any idea what could be causing this?

    Probably a stuck key or some other hardware issue with your keyboard.

  • Subject: Displaying key figures without currency unit in BEX reports

    Hello,
    Is it possible to display key figures without currency unit in BEx reports? For example; Material Costs 1000 EUR
    should be shown as 1000. Currency unit EUR and also scaling factor (*1000 EUR) does not interest us.
    When we use display options in query properties, we just see an option of displaying key figures with/without scaling factors, which does not solve our problem.
    Thanks for your reply.
    Regards,
    Nuran Adal

    Hi Eugene,
    thank you very much for your quick reply. We have very detailed reports with min. 10 columns, I should have defined 10 more formula columns to realize this (NODIM), but anyway there is no other way to do this, am I right?
    Best Regards,
    Nuran

  • Remove the key listener from JTable problem please help

    Hi
    I�m trying to remove the key listener from my table, it doesn�t work,
    I�m pressing the enter key it still goes to the next row from the table,
    hear is my code
    KeyListener[] mls = (KeyListener[])(table.getListeners(KeyListener.class));
    for(int i = 0; i < mls.length; i++ )
    table.removeKeyListener(mls);
    Please help thanks

    Hm ...
    that should indeed remove all the KeyListeners from your table - the question is only, when does this happen?- Where in your code do you remove the KeyListeners?- I am quite sure, you remove them successfully, but after your have removed them, one or more KeyListeners are added to the table.
    greetings Marsian

  • Generate Key Events without pressing key???

    Hi,
    Is it possible to generate key events without pressing a key in swing???
    i dont know if the question is logical or not, but just i want to generate some display as if the key is pressed by the user
    Ashish

    assuming c represents a text field.
    This will type the character 'a' in the text field:
    c.dispatchEvent( new KeyEvent (c, KeyEvent.KEY_TYPED, 0, 0, KeyEvent.VK_UNDEFINED, 'a') );
    This will invoke Ctrl+F1, which will show the tool tip for the text field:
    c.dispatchEvent( new KeyEvent (c, KeyEvent.KEY_PRESSED, 0, KeyEvent.CTRL_MASK, KeyEvent.VK_F1) );

  • Wrong line in key listener

    Hello
    I m writing a key listener to change label 1 to blue when the one key is pressed and change to grey when another key is pressed. Here is my code for it so far.
    if (evt.getKeyChar() == '1') {
    process(evt.getKeyChar(lbl1.background = 51,255,255));
    else if (evt.getKeyChar() != '1'){
    process(evt.getKeyChar(lbl1.background = 240,240,240));
    The second line starting with process to change the label colour does not function correctly but the first one does which gets the character. The label is a swing label but would it be best to change the label to a awt label to get the colour change to work using java.awt.colour? Also could I take out the .background part and just have lbl1= 51,255,255,255. Thanks ahead for any help with the second line.
    Nick

    NUFCjavaworker wrote:
    Cheers for the help however it has made another problem. The shorter version worked the best so that is what I used. I implemented it like this:
    public class MyKeyListener extends KeyAdapter {
    public void keyPressed(KeyEvent evt) {
    lbl1.setBackground(evt.getKeyChar() == '1' ? Color.BLUE : Color.GRAY);
    lbl2.setBackground(evt.getKeyChar() == '2' ? Color.BLUE : Color.GRAY);
    lbl3.setBackground(evt.getKeyChar() == '3' ? Color.BLUE : Color.GRAY);
    lbl4.setBackground(evt.getKeyChar() == '4' ? Color.BLUE : Color.GRAY);
    lbl5.setBackground(evt.getKeyChar() == '5' ? Color.BLUE : Color.GRAY);
    It went on like that until I had cover all the characters and all the labels, I wanted to cover. Here is the problem that occured. The code that is generated when you create a java desktop application using the latest version of netbeans now has errors in it. Do I have to import any other java events or items that will solve this problem.
    NickErrors you say?

Maybe you are looking for

  • Windows 2008 Server R2 Windows Explorer Refresh Issue

    I work on a Windows 2008 R2 server that I connect to through remote desktop. Recently the automatic refresh in Windows Explorer stopped working. If I create, delete, copy, move, rename etc., I do not see the changes unless I refresh (F5). I tried the

  • OUTPUT TYPES FOR SALE ORDER(VA01)

    Hi All, When i create a sale order, the data can be downloaded thru output type BA00 & standard program RSNASTED into SAPScript, but i wanted to download it to internal table. Can u please tell me the changes to be made after copying the standard pro

  • IPhoto 8.1.2: After Crash, Can't Open Library

    I had just transferred several photos from the iPhone to my MacBook Pro (on OS 10.7.5).  The transfer was successful, and when I look at my iPhoto libary folders, the pictures are where they ought to be, so I'm not worried about losing them.  But wit

  • Can't make phone calls on i6

    Initiated my new i6 after backing up from my 5c. When I try to make a phone call now I'm automatically redirected to an outside operator that's going to require me to use a credit card to make my call. Under Settings when I go to Phone it says my Pho

  • Sizing error

    Okay, so I have this formatting code: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Gui1{      JTextArea area;      JScrollPane area0;      JTextField field;      JTextField field2;      JPanel pan