No keyboard Events on a Window

Hello Java community,
I can't understand why, if you build a simple Window (not a Frame), all keyboard Events are simply ignored !! Everything acts as if the Window can't get the focus, because no LOST_ or GOT_ FOCUS events are triggered. This means that you can't handle keyboard events with a Window and therefore a Textfield will not work (however it still receive mouse events ??). BUT it perfectly works with a Frame , which extends from Window! Strange isn't it ?
I've heard on other forums that , to be eligible for focus a Window must have:
- its owner Frame (the one given in the constructor) :
* displayed on screen,
* containing a focusable component.
- itself, containing a focusable component.
thus i respect theses limitations, i still can't get any keyboard events
pleaze help, i am writing a text area component and without keyboard events ...

Thanks for the answers BUT,
I have already read the javadoc in every ways and I am sure I respect perfectly all the conditions to have my WINDOW focusable.
No, there is another problem, a strange one. In fact, my program works fine under Windows I can type characters into my Textfields but once executed under Linux : no keyboard events! Therefore, no character type allowed. I display in the console, all events that are incomming into my Window, and ALL events are ok (mouse, windows..) except keyboards ones. When I press a key, nothing appears in my console, that's why I think keyboards events are caught and not dispatched to the Window.
I noticed an interresting fact when using my app in Applet. For example, I launched my application with Mozilla (Netscape 4, JRE 1.1). I displayed an empty Window on screen, when my mouse passes over it or when I click, the Java console showed me these events. When I try to hit some keyboard keys, nothing except for certain keys such as F, G, D, J and numeric keys. In fact, these keys are shortcuts to some of the System class (F calls Finalization, G=Garbage Collector, D=Debug Mode...). I truely think that on a Window, all keyboard events are send to the console. Another fact that proved me that i was right : I tried my app with JRE 1.2, and when I wanted to type characters into my TextField (which is on my Window), the typed characters appeared in the console !!!!
A JAVA-experimented person told me to redirect the standard input to my own inputStream... but how ?
Any ideas of solutions ?

Similar Messages

  • Keyboard events in borderless windows.

    I have created a borderless window (class JWindow) in order to handle transparency (non-rectangular window). While the whole system seems to work, the keyboard events are not forwarded anymore to components within the borderless window. For example, there is a list in the window that could be browsed using up/down on the keyboard; it is not possible to use the keyboard to browse it anymore now (although the mouse can hopefully still be used).
    I didn't find anything about that in the bugs database. Do someone has an idea?
    Thanks in advance :-)

    Wow, it looks like a ugly hack! But it seems it works despite of a few problems: when I click, it sometimes has some kind of painting loop that lasts until the mouse leaves the window, and when I press a key, the paint events are sent only when the mouse enters/leaves the windows. Very strange, but at least I have somewhere to look. Thanks :-)

  • [solved] send keyboard event / key to specific background window?

    Hi!
    So... is this possible / how? I'd like to send keyboard events to a window in the background - maybe even in regular intervals. While I'm dong something else, in another window (or nothing).
    Thx!
    Last edited by whoops (2011-07-21 11:05:44)

    Actually I'm thinking, maybe there is a totally another (better?) way to do what you want. I usually find that automatizing tasks in linux differs very much from the windows experinece, most of the times scripting is the most powerful and easy way of doing things. So if you describe to us what you want to accomplish, maybe some experienced linux gurus would give you some advices that sets you off course from sending keys and mouse events. I used to do that in windows with autohotkey, but now in linux there was no need for that so far ever, except for using system wide abbreviations with autokey.

  • Capture All Mouse and Keyboard events even if java window NOT in FOCUS

    Hi All,
    Is this possible to capture all mouse and keyboard events even if java window is not in focus or is minimized. This java program should cature each and every event from the user once the program is started. Any help in this regard is appreciated!
    Thanks&Regards
    Sam

    I don't think you can. (without JNI)

  • Handling keyboard events in applets?! Possible?!

    Hello,
    I wrote a little game that uses keyboard handling events. It is an applet that can also standalone as an application. When i run the program as a standalone application, it handles keyboard inputs fine. ie...i can press the left and right arrow keys and something happens.
    But when i run my program as an applet in a web browser, the applet starts up correctly but does not handle any keyboard events. It only handles mouse events. For instance, if i press the up and down arrow keys, the browser window scrolls up and down!!! And the up and down arrow keys have a specific purpose in my applet.
    QUESTION: how do i get my applet to accept keyboard inputs such as UP,DOWN, LEFT, RIGHT?

    Hmm something went wrong w/ my post so i hope this doesnt show up twice.
    Hey thanks a lot for helping me out. The applet/application is large so here is the main() method and the keyboard event handling class code. My question is how come the keyboard events get properly handled when i run it as a standalone application, but keyboard events go to the browser when i run it as an applet? I would post my entire code but it's over 1000 lines and spread out over 8 files. hehehe.
    Here is main():
      public static void main(String[] args)
          MyProgram applet = new MyProgram();
          applet.isStandalone = true;
          JFrame frame = new JFrame();
          frame.setTitle("Physics: Kinematics");
          frame.getContentPane().add(applet, BorderLayout.CENTER);
          applet.init();                        // initialize the applet inside frame
          applet.addKeyListener(kbHandler);     // kbHandler is a keyboard handling object
          applet.start();
          frame.setSize(APPLETWIDTH,APPLETHEIGHT);
          Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
          frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
          frame.setVisible(true);
          // private inner class to terminate the Application when frame closes
          frame.addWindowListener(
            new WindowAdapter()
                public void windowClosing(WindowEvent e)
                    System.exit(0);
          frame.show();
       }"kbHandler" is the keyboard event handling object. It gets initialized in the applet.init() method.
    kbHandler = new BMkeyboardHandler();And here is the keyboard event handling code:
    // ===== keyboard event handler ===========
       private class BMkeyboardHandler implements KeyListener
            public void keyPressed(KeyEvent e)
                pushedKey = e.getKeyCode();
                // General administrative key actions
                if(pushedKey == KE.VK_H)
                    actionArea.toggleHelpMenu();
                    actionArea.repaint();
                else if(pushedKey == KE.VK_C)
                    actionArea.toggleCtrlMenu();
                    actionArea.repaint();
         }So there it is. Again, the program handles events fine as a standalone application. But running it as an applet in a browser (and even the appletviewer) doesnt let me handle keystrokes.

  • Detect mouse clicks or keyboard events on desktop or everywhere

    Hi,
    What I have to do is to start the application minimized in the system tray. Then the application must be listening for crtl+shift+left mouse click in any part of the desktop or an opened application, when that happens, I have to show a window asking if the user want to take a screenshot starting in the x,y point he clicked and then take the screenshot.
    What I don't know how to do is to detect the mouse and keyboard events when the application is supposed to be running minimized in the system tray.
    How can I do that? Can anybody give me a hint?
    Thanks in advance.

    It's not possible with plain Java. You will need native code for this. Take a look at JNA.

  • Implementing keyboard control on a window

    Hi, I'm currently trying to implement keyboard control for a tetris game I'm attempting to make, but I've done it a rather dodgy way. I've implemented it on a Jpanel instead.
    What I want to do is, implement the keyboard control on the window, however I've been unable to do so. I'm new to java (this is quite an ambitious project - but I'm trying to learn) so any help would be really appreciated.
    This is the code for constructing the window with the Panels:
    please note: I have omitted the import statements.
    public class MainWindow extends JFrame
        public MainWindow()
            setTitle("Tetris!");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            Container contentPane;
            contentPane = this.getContentPane();
            contentPane.setLayout(new BorderLayout());
            Game game;
            game = new Game(10, 20);
            GameView gameView = new GameView(game);
            contentPane.add(gameView, BorderLayout.CENTER);
            InfoPanel infoPanel = new InfoPanel(game);
            contentPane.add(infoPanel, BorderLayout.EAST);
            KeyboardControl keyboardControl = new KeyboardControl(game);
            contentPane.add(keyboardControl, BorderLayout.SOUTH);
            this.pack();
            game.start();
    }This is the code for the keyboard control:
    public class KeyboardControl extends JPanel implements KeyListener
        private Game game;
        public KeyboardControl(Game game)
            this.game = game;
            setLayout(new GridLayout(0,-1));
            setFocusable(true);
            addKeyListener(this);
        public void keyPressed(KeyEvent event)
               int key = event.getKeyCode();
                if (key == event.VK_LEFT)
                    // Move the block left
                    game.moveLeft();
                    else if (key == event.VK_RIGHT)
                        // Move the block right
                        game.moveRight();
                        else if (key == event.VK_UP)
                            // Rotate the block
                            game.rotate();
                            else if (key == event.VK_DOWN)
                                // Drop the block
                                game.drop();
        public void keyReleased(KeyEvent event)
        public void keyTyped(KeyEvent event)
    }Can anyone provide any hints and sites to look at? I've been through the java tutorials, and the above is what I have achieved.

    Your implementation of the keyboard control (or listener) looks correct.
    To change it to listen to keyword on the window, would require only a
    small change.. See below code.
    public class MainWindow extends JFrame implements KeyListener
        public MainWindow()
            setTitle("Tetris!");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            Container contentPane;
            contentPane = this.getContentPane();
            contentPane.setLayout(new BorderLayout());
            Game game;
            game = new Game(10, 20);
            GameView gameView = new GameView(game);
            contentPane.add(gameView, BorderLayout.CENTER);
            InfoPanel infoPanel = new InfoPanel(game);
            contentPane.add(infoPanel, BorderLayout.EAST);
           addKeyListener(this);      
            //KeyboardControl keyboardControl = new KeyboardControl(game);
            //contentPane.add(keyboardControl, BorderLayout.SOUTH);
            this.pack();
            game.start();
       public void keyPressed(KeyEvent event)
               int key = event.getKeyCode();
                if (key == event.VK_LEFT)
                    // Move the block left
                    game.moveLeft();
                    else if (key == event.VK_RIGHT)
                        // Move the block right
                        game.moveRight();
                        else if (key == event.VK_UP)
                            // Rotate the block
                            game.rotate();
                            else if (key == event.VK_DOWN)
                                // Drop the block
                                game.drop();
        public void keyReleased(KeyEvent event)
        public void keyTyped(KeyEvent event)
    }

  • Movieclip.x movement using keyboard event

    Hi, im working on a game project and i have a question about movie clip movement using keyboard event.
    Basicly i have a character on screen and it can move on the x axis using the left and right buttons.
    Im making my charater move by changing the x value of the character movieclip but i find very it laggy and not smooth and if im going point by point
    then it's too slow.
    Whats the best way to make the character move so that the transition will be smooth.

    Ok obviously you've left out where you add your eventListener, i'll assume that's a KEY_DOWN event. When the user holds down a key, that key event is triggered once immediately and then after a pause it is triggered at regular intervals. Check this out by holding down the up/down arrow and watching this browser window scroll.
    That's not the movement you'd be looking for when moving a character in a game. Instead, you'd be setting a variable on KEY_DOWN to indicate the character direction - it could be an integer for example that you increase by 1 if the user pressed RIGHT or decrease by 1 if the user pressed LEFT.
    In addition to your KEY_DOWN eventListener, you could have an ENTER_FRAME and  KEY_UP eventListeners set up:
    The enterFrame event handler could move the character in the direction specified in your direction variable. This is where you might ensure your character doesn't go beyond its limits in both directions.
    In the keyUp event handler, you could do the converse of what you did in the keyDown handler(increase by 1 if the user released LEFT, decrease by 1 if the user released RIGHT).

  • Capture a keyboard event on af:panelGridLayout?

    Hi,
    <af:panelGridLayout> currently supports a client listener with mouse events.
    But I have a requirement where I need to capture keyboard events.
    I tried adding an invisible inputText component on the page and focus on it onclick of the panel.
    My assumption was that when the inputText is on focus, it should be able to capture keyboard events even though its invisible.
    This does not work. Please see code below. The "showAlert" javascript method never gets called.
    <af:inputText clientComponent="true" visible="false" value="dummy" id="keyLink">
         <af:clientListener method="showAlert" type="keyUp"/>
    </af:inputText>
    <af:panelGridLayout id="pgl3">
        <af:clientListener method="toggleRowSelection" type="click"/>
    </af:panelGridLayout>Javascript code:
    function showAlert(event) {
        alert("javascript event " + event.getKeyCode());
    function toggleRowSelection(event) {
        var source = event.getSource();
        var keyLink = source.findComponent("::keyLink");
        keyLink.focus();
        callServerMethod(event);
    };How do I achieve this?
    -Anitha

    "[email protected]" <[email protected]> wrote in message news:[email protected]..
    I have figured out how to add menu Item to an existing EXE program, but I have not yet been able to figure out how to capture there events.&nbsp; Any help would be greatly appreciated.
    It's not entirely clear what you are trying to acieve. I think you're trying to add menu items to an exsisting exe without recompiling it, from LabVIEW. If so the following applies.
    You have to hook the winproc. When a menu item is selected, windows send a message to the window's winproc. There are some API's that can be used to point the address of the winproc to another routine. This routine can do filtering, and then call the original routine.
    Note that LabVIEW doesn't (or didn't until LV7) use windows menu's, so when a LabVIEW (or exe created with LabVIEW) menu item is called, windows will not send anything. That is the price for platform independency.
    I think the OpenG site (or perhaps Winutils from NI) has some vi's to hook windows messages that are send to LabVIEW. Perhaps you can also use them hook another application.
    Regards,
    Wiebe.

  • Lock all mouse and keyboard event.

    i am looking ways to lock up all the mouse and keyboard events in windows...do u guy have ways to solve this?

    Do you mean that people should not be able to start other applications, or switch to other running applications, before they have typed the right password into you java program? I don't think that can be done in java - it requires more control over the computer than Java typically gives you. You may have some success with full-screen AWT, though.
    But why not use log-on-systems incorporated in whatever operating system you are using? Or some other netware thingie? Honestly, I think you are trying to reinvent the wheel, and even with tool that's not very appropriate.

  • Keyboard events in jsp  that working in Mozila fire fox Browser

    hai cutes
    have a peace day.
    i did some keyboard event project using JSP
    that can be nicely working in Internet Explorer
    But i want to working in Mozila fire fox Browser and other browser also...
    thank u

    You didn't do it using JSP, keyboard events are client side and will need to be done with javascript. AFAIK the only thing about events that is not cross-browser is the way the event object is handled. This can easily be fixed with a minor workaround.
    <script>
    function handleEvent(e)
    // this is the trick
    if(e == null){
      e = window.event;
    if(e.keyCode == 13) { // enter
    </script>
    <input type="button" onKeyDown="handleEvent(event)">That should make it work in multiple browsers. Well it least it works for me :)

  • Keyboard events going to InDesign and not on textbox

    Hi,
    I have ported a project from InDesign CS2 to InDesgn CS3 on Mac. I have dialogs in rsrc file (not converted to nib file) which I am also using on XCode.
    Now the problem which I am facing is that when typing in a textbox then keyboard events are going to InDesign and only those events which are not handled by InDesign is received by my textbox.
    Has anyone experienced the same problem before?
    Any help will be appreciated.
    Regards,
    Rahul Rastogi

    Start Firefox in [[Safe Mode]] to check if one of your add-ons is causing your problem (switch to the DEFAULT theme: Tools > Add-ons > Themes).<br />
    See [[Troubleshooting extensions and themes]] and [[Troubleshooting plugins]]<br />
    <br />
    If it does work in Safe-mode then disable all your extensions and then try to find which is causing it by enabling one at a time until the problem reappears.<br />
    You can use "Disable all add-ons" on the [[Safe mode]] start window to disable all extensions.<br />
    You have to close and restart Firefox after each change via "File > Exit" (Mac: "Firefox > Quit"; Linux: "File > Quit")

  • Responding to keyboard events when focus is lost

    Is there any way to have a Java Application respond to keyboard events if the focus has been lost?

    Media programs respond to keyboard events to turn up
    the volume, etc... and the program does not have to
    have the focus to do this.On the computer I'm using here, the keyboard button
    to raise/lower the volume goes to the OS's volume control.
    Not to an arbitrary application.
    So the situation in your analogy is not mandatory.
    And more generally, even if your app is not hidden,
    users do not expect that it will "steal" keystrokes.
    Let's say I download a very cool "World Clock Display"
    program that displays the world time of New York
    and Tokyo in the background.
    Let's say I then go to my bank's website.
    I do not want that the World Clock Program
    (which is in full view) to capture my bank password.
    (Clarification: on Windows, on Linux... it is
    actually perfectly trivial for a C/C++ program
    to capture all keystrokes in the background.
    But I do not know if you can do it using Java or not)

  • How to receive a keyboard event (arrowkeys)

    Hi,
    I'm a bit confused about how to setup event-handling for keyboard event. I have created a window with a File's Owner called "MyDocument". But if I implement the method
    <pre>
    -(void)sendEvent: (NSEvent *)theEvent {
    printf("Called\n");
    </pre>
    it wont be called. Does anyone know what else I have to do in order to receive keyboard events when a user presses an arrowkey inside my application ?
    Macbook Pro 15''   Mac OS X (10.4.8)   2GHz, 2GB

    You most likely need to implement -(void)keyDown:(NSEvent *)event to catch keyboard events and then query event for the key pressed. This method is defined in NSResponder together with mouseDown:, mouseDragged, keyUp:, etc.
    Here's a couple of examples from two sample apps:
    'Dicey' (online sample code):
    <pre>
    -(void)keyDown:(NSEvent *)event {
    // If the space bar was pressed, toggle hold on the first responder
    if ([[event characters] isEqualToString:@" "]) {
    [[dice objectAtIndex:firstResponderIndex] toggleHold];
    [self setNeedsDisplay:YES];
    } else {
    // We do care about our superclass dealing with tab shift-tab for first responder changes
    [super keyDown: event];
    </pre>
    'Worm' (From: /Developer/Examples/AppKit/Worm):
    <pre>
    - (void)keyDown:(NSEvent *)event {
    NSString *keys = [event charactersIgnoringModifiers];
    wormHeading = kGameHeadingStraight;
    if (keys && [keys length] > 0) {
    unichar c = [keys characterAtIndex:0];
    if (c == NSLeftArrowFunctionKey) {
    wormHeading = kGameHeadingLeft;
    } else if (c == NSRightArrowFunctionKey) {
    wormHeading = kGameHeadingRight;
    </pre>
    Note the use of NSLeftArrowFunctionKey for example in Worm. These and many other 'special' key constants are defined in NSEvent.

  • SWT_AWT mouse/keyboard event trouble

    Hello everyone,
    I have created an ActiveX Component with Visual C++ that uses a WEmbeddedFrame to contain a Swing based GUI. I have copied the code from the SWT_AWT class in the SWT toolkit and it worked fine up to JDK 1.5.0_03.
    Starting with JDK 1.5.0_04 the Swing GUI does no longer receive Mouse/keyboard events.
    I have read through all available material concerning this problem and I have tried the two possible solutions that these documents suggest:
    - called addNotify() on the embedded frame
    - inserted a heavyweight container (java.awt.Panel) between Embedded Frame and the Swing GUI
    However, no luck. The GUI displays fine but does not respond to user actions.
    Does anybody have an idea what might have changed from 1.5.0_03 to 1.5.0_04, that could help me solve this puzzle?
    Thank you very much in advance,
    Helmut

    SWT works up to 1 tree and two text components only. If you have more than that, you windows will get tainted. Basically it's useless to wirte any useful programs for Windows. You never get comaptible latest look from it both mac and windows. Don't waste time for learning it. You better spend time for learning other things. If AWT/Swing is not good enough, learn native languages such as Visual C++/Basic! I have to say SWT wasn't meant to give us better programming platform!

Maybe you are looking for