Key events on the glasspane

I'm trying to develop a glasspane that captures the key events as well as the mouse events.. In this example, the mouse events happen exactly the way I want them to, but I cannot for the life of me get a key event to register.. I've tried capturing keyTyped(), keyPressed(), and keyReleased() to no avail.. Dukes to whoever shows me the light,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BCInv
    extends JFrame {
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea jTextArea1 = new JTextArea();
  JScrollPane jScrollPane2 = new JScrollPane();
  JTable jTable1 = new JTable();
  JButton jButton1 = new JButton();
  public BCInv() {
    jbInit();
    setVisible(true);
    getGlassPane().setVisible(true);
    //here I'm adding both listeners in the same manner
    getGlassPane().addMouseListener(new BCInv_mouseAdapter());
    getGlassPane().addKeyListener(new BCInv_keyAdapter());
  public void jbInit() {
    setSize(new Dimension(611, 423));
    getContentPane().setLayout(null);
    jScrollPane1.setBounds(new Rectangle(9, 275, 574, 110));
    jTextArea1.setText("jTextArea1");
    jScrollPane2.setBounds(new Rectangle(10, 13, 573, 175));
    jButton1.setBounds(new Rectangle(196, 217, 89, 42));
    jButton1.setText("jButton1");
    this.getContentPane().add(jScrollPane1, null);
    this.getContentPane().add(jScrollPane2, null);
    this.getContentPane().add(jButton1, null);
    jScrollPane2.getViewport().add(jTable1, null);
    jScrollPane1.getViewport().add(jTextArea1, null);
  public static void main(String[] args) {
    BCInv BC = new BCInv();
  //this key stuff here never executes..
  void BCkey(KeyEvent e) {
    System.out.println("A key was typed: '"+e.getKeyChar()+"' = "+e.getKeyCode());
  class BCInv_keyAdapter extends java.awt.event.KeyAdapter {
    BCInv_keyAdapter() {}
    public void keyTyped(KeyEvent e) {
      BCkey(e);
  //All this mouse stuff works great..
  void BCmouseClicked(MouseEvent e) {
    System.out.println("click");
    Point glassPanePoint = e.getPoint();
    boolean inButton = false;
    boolean inMenuBar = false;
    Component component = null;
    Container container = getContentPane();
    Point containerPoint = SwingUtilities.convertPoint(
        getGlassPane(),
        glassPanePoint,
        getContentPane());
    component = SwingUtilities.getDeepestComponentAt(
        container,
        containerPoint.x,
        containerPoint.y);
    if (component == null) {
      return;
    System.out.println("underlying component="+component.toString());
  class BCInv_mouseAdapter
      extends java.awt.event.MouseAdapter {
    BCInv_mouseAdapter() {}
    public void mouseClicked(MouseEvent e) {
      BCmouseClicked(e);
}

* @author nordj
public class GlassPane extends JPanel {
     private boolean okToLooseFocus = true;
     public GlassPane() {
          setName("GlassPane");
          setVisible(false);
          super.setOpaque(false);
          //super.setBackground(new Color(0x00,0x00,0x00,0x88));
          //super.setForeground(new Color(0x00,0x00,0x00,0xFF));
          // trap keyboard & mouse events.
          //enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
          enableEvents(0xFFFFFFFFFFFFFFFFl);
          setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
          setInputVerifier(new GlassPaneInputVerifier());
     /* (non-Javadoc)
      * @see java.awt.Component#processMouseEvent(java.awt.event.MouseEvent)
     protected void processMouseEvent(MouseEvent e) {
          if (e.getID() == e.MOUSE_CLICKED) {
               Toolkit.getDefaultToolkit().beep();
               e.consume();
          super.processMouseEvent(e);
     /* (non-Javadoc)
      * @see java.awt.Component#processKeyEvent(java.awt.event.KeyEvent)
     protected void processKeyEvent(KeyEvent e) {
          Toolkit.getDefaultToolkit().beep();
          super.processKeyEvent(e);
     /* (non-Javadoc)
      * @see java.awt.Component#processEvent(java.awt.AWTEvent)
     protected void processEvent(AWTEvent e) {
          // TODO Auto-generated method stub
          //System.err.println(e);
          super.processEvent(e);
     /* (non-Javadoc)
      * @see java.awt.Component#setVisible(boolean)
     public void setVisible(boolean visible) {
          okToLooseFocus = !visible;
          super.setVisible(visible);
          if (visible) {
               requestFocusInWindow();
     /* (non-Javadoc)
      * @see java.awt.Component#paint(java.awt.Graphics)
     public void paint(Graphics g) {
          g.setColor(new Color(0xFF,0x00,0x00,0x88));
          g.fillRect(0,0,getWidth(), getHeight());
      * Simple hack to stop the RootPane loosing focus when it's visible
      * @author nordj
     private class GlassPaneInputVerifier extends InputVerifier {
          /* (non-Javadoc)
           * @see javax.swing.InputVerifier#verify(javax.swing.JComponent)
          public boolean verify(JComponent input) {
               //return (!isVisible());
               return okToLooseFocus;
}

Similar Messages

  • Detect key events on anything

    If I have a JFrame with many components on it, how do I detect if a key is pressed, without having to add a listener to every component? For example, how can I check for the escape key being pressed, but not having to have componentN.addKeyListener(this); for everything?
    Thanks
    US101

    The correct way to collect the key events is the following:
    from java 1.2 to java 1.3 use the FocusManager. This class can be found in the javax.swing package. You would then need to extend the DefaultFocusManager and override specific behavior.
    for java 1.4 use the KeyBoardFocusManager. This class can be found in the java.awt package.
    Please see the following for a better explanation:
    http://java.sun.com/j2se/1.4/docs/api/java/awt/doc-files/FocusSpec.html
    Thanks for your time,
    Nate

  • Binding Mouse and Key Events.

    Hi,
    When a user clicks the Ctrl + V the data from the clipboard
    is pasted into the user area. Instead of using this can we do it
    using a mouse click. For now, I am planning to bind the event code
    of these combinations (CTRL + V == 86) to the mouse click. But when
    I tried this I am unable to bind this key event to the mouse event.
    Can anyone help me out for getting out of this problem.
    Regards,
    eodC.

    No, it isn't.
    Kind regards,
      Levi

  • Handling Key Event

    How to handle key events in the Console?
    Plz give sample code for it.
    Thanks in advance.

    Don't have any experience with it personally, but I think curses does this type of thing http://sourceforge.net/projects/javacurses/

  • Simulate Key Event " "

    Does anybody know how to simulate keys that are only available while holding down the shift key? I have a VI that is based on an API that simulates key events, including the shift key. It works great. But I do not know how to get characters, such as ">", that are combinations of the shift key. It seems to me that just sending the code for the shift key and then the code for "." generates two events and only results in the "." since the shift key was not kept down... Any help in this issue is very welcome!! Thanks! /Mikael (LV 6.1)

    Look up an ASCII chart. The ASCII code for ">" is 62. There is a "keybd_event" function inside user32.dll. It's syntax is:
    void keybd_event(unsigned char bVk, unsigned char bScan, unsigned long dwFlags, unsigned long dwExtraInfo);
    Use the Call Library Function Node to call this function. Wire the ASCII code (62) to the first U8 input, and 0 to the next three inputs. This simulates the key being pressed. Then pause for a few milliseconds. Then call the function again, wire the ASCII code to the first U8, wire 2 to the first U32, wire 0 to the other U8 and U32. This simulates releasing the key. You can simulate any keystroke by using this function and the ASCII code. You must use it twice, once for key down and once for key up.
    - tbob
    Inventor of the WORM Global

  • Capturing Enter key event from text box

    In my application, there are a number of text boxes. I want to create a feature where an enter key even is seen and specific function executed depending on which text box was in focus when the enter key was pressed. Currently I am able to capture an enter key event on the whole page. How do I do that specifically for a text box?
    This application is done in HTML, javascript and Adobe AIR.
    Thanks
    Binu
    www.verseview.info

    Hi
    That's true that Flex4.5 SDK under Flash builder Burito doesn't listen Enter key in Text Area for KeyUp or KeyDown event.
    Here I found a work around you can use.
    Add an eventlistener to the component in actionscript and set the useCapture=true next to eventlistener function. Now you can listen enter key for KeyUp or KeyDown Event
    Hope this helps
    Rush-me

  • Can't capture key event in applet

    Hi,
    Currently i'm developing an applet. However, i fail to capture the key event in the applet. The key that i fail to capture is VK_TAB. Any idea to handle this?
    Thanks in advance!

    to capture key event, u need to
    1. addKeyListener
    2. focus on that component (yourApplet must focusable)

  • How to catch key events

    Hello!
    I have a component which extends JPanel. Now I dynamically add a JTextField as a child to this component. The problem is: when the textfield has focus, how can I get key events in my panel before the textfield gets them (and maybe intercept them so that the textfield doesn't get them at all)?
    Background: the component is a self written table and the textfield is an editor. Now I have to make sure that when I am editing and press e.g. "cursor up", that not the textfield will get this key event but the table (which should traverse the current cell then ...)
    The problem is: I cannot change the textfield (extend it or something) because a possible solution has to work with any java awt "Component" (or at least with JComponent).
    Any help very appreciated.
    Michael

    Hello,
    implement the keyListener interface for the Extended component...
    and in Keypressed method
    keyPressed(keyEvent){
    // do all ur reuirements here
    //and comsume the keyEvent...
    hope this helps

  • Key event capture

    I can't find a way to capture the key events of the special key on multimedia keyboards, just like play/pause, browser or music..
    Any ideas?

    You probably can't. They have their own drivers to handle those specialties and Java (as far as I know) doesn't have support for those.

  • In contacts there is the possibility to add a new event, as the birthdays, but they do not appear in iCal. Is there any way to make that possible? It is normal to have a person with his birthday, anniversary and others key dates you want to link to him.

    In contacts there is the possibility to add a new event, as the birthdays, but they do not appear in iCal. Is there any way to make that possible? It is normal to have a person with his birthday, anniversary and others key dates you want to link to such person, but the only one shows up is the birthday. How to be able to show all those dates linked to people in the agenda in the iCal?
    Thanks

    Hi,
    I sugggest you try my application, Dates to iCal. It is shareware with a 2 week trial period.
    Dates to iCal 2 is a replacement for Apple's birthday calendar for iCal. It has a range of features to allow the user to choose what, and what not, to sync to iCal from Address Book.
    As well as automatically syncing birthday dates from Address Book, Dates to iCal 2 can sync anniversary and custom dates. It can set up to five alarms for each date in iCal and can also set different alarms for birthdays and anniversaries. It allows the option of only syncing from one Address Book group. This application also allows for the titles of the events sent to iCal to be modified to the user's preference.
    Best wishes
    John M
    As I sell software on my site and ask for donations, the Apple Support Communities Use Agreement requires that I state that I may receive some form of compensation, financial or otherwise, from my recommendation or link.

  • Photos will not allow me to set the key picture for the album or event

    Photos will not allow me to set the key picture for the album or event - it is set to grey !
    Anyone else had this and found why ?
    new PHOTO app replacing iPhoto bugs/ Problems OTHER PROBLEMS :
    - no longer has a five star rating system
    - my keywords for selecting albums are gone.
    - cannot copy location of one photo to another
    - cannot have an album with  keywords AND rating > a value
    - cannot set key photo of album/event
    - multi parameter search feature gone
    Anyone got any ideas how to get around this terrible downgrade of the software ?
    BTW I have just discovered that iPhoto and its' libraries are still there, so I can go back but be left behind and frozen in time on this version if I do.
    Photos now take up 700Gb whereas it was 350Gb  #@!$#@!@#!@#
    Thanks

    That are all good points - but Photos is a completely new application. and it is only version 1.
    Continue to use iPhoto, girraween; the next update to Photos will probably add a few more features.
    iPhoto 9.6.1 is working very well with MacOS X 10.10.3.
    The migration let iPhoto installed and also your iPhoto Library will still work with iPhoto.
    If you did not uninstall iPhoto, it will still be there in your Applications folder.  And if you had updated to the latest version iPhoto 9.6.1 before you upgraded to MacOS X 10.10.3,  you can simply drag iPhoto back to the Dock from your Applications folder and continue to use it. Double click iPhoto and confirm, that you want to open iPhoto and not Photos.   It is not compulsory to use Photos, while it it still the first release. When you launch iPhoto, you will see a warning, that the iPhoto Library has been migrated to Photos, but simply click the "Open iPhoto" button.The library will still work with iPhoto. ()see: If Photos won't open a library that you already migrated - Apple Support
    If your iPhoto version is not iPhoto 9.6.1, you need to update it.
    See this User Tip:
    Usually it will work to reinstall instead of updating:
    Get iPhoto 9.6.1 if you didn't update before OS... | Apple Support Communities
    The main idea is to move the iPhoto app to the Trash, so Spotlight does not see it as installed, reload the AppStore, and then to try to download again from the App Store - from your Purchases tab, not from the main page, since you can neither buy iPhoto nor update it, but you can reinstall, if your AppleID is associated with it.
    Meanwhile you may want to send a feature request to Apple using the feedback form. Apple - Photos - Feedback
    I hope, Apple will listen and add more features to Photos to make it usable for large Photo Libraries.

  • My Iphoto library has recently updtaed itself with a same picture icon for every event. I backed up all my photos and deleted all the events in the library. When I imported  a fresh batch of pictures the same picture appeared as the key photo. Help

    Hi,
    My i photo events in the library have updated themselves with a key photo. I really do not want this picture as a key photo on every event.
    I backed up and deleted my entire library. After importing a fresh event, the key photo still remained the same. How can I stop that. 

    Sorry to ask but since we onlhy have words to comunicate with here using them correctly is critical
    iPhoto is an application and lives in your Applications folder
    Your photos are in a database which by default is names iPhoto library and is in your pictures folder
    I still have Iphoto on my computer.
    Is this the application of the intact non-functioning liPhoto library?
    I have copied all the pictures on a USB so I wont loose any data.
    exactly how did you do this?
    Then I deleted all the pictures.
    and exactly how did you do this?
    Have you done this?
    If so what were the results?
    Back up your iPhoto library, Depress and hold the option (alt) and command keys and launch iPhoto - rebuild your thumbnails (you may need to do several times)
    LN

  • Catching Alt Key Press with the Key Down Filter Event

    I am writing an application that requires specific key combinations using ctrl, shift, and alt in addition to a function key (F1, F2, F3, etc).  The application works great except for when I try to catch an alt key press.  The alt key press does not seem to fire an event eventhough it is an option in the PlatMods cluster as well as the VKey enum.  When I press the alt key when my application is running the cursor changes to a normal mouse pointer from the usual finger pointer and prevents any other key presses from going through (in addition to not firing an event itself).
    I have tried completely removing the run-time menu, which doesn't seem to help.  I currently discard all keys after I handle them in my event structure.
    I really hope that the only solution isn't using a Windows DLL.  Any suggestions or ideas at all would be greatly appreciated.
    Thanks,
    Ames

     Hi Ames
    As Kileen has said Khalid has already given you a good solution to detect the ALT key.
    I have another approach that might let you stick to your event-driven approach. I suggest that you have another loop in your app that polls the keyboard using the Input Device utility vi's. When this poll loop sees an ALT + KEY combo it raises a dynamic user event and will be processed in your event structure. This means you can keep your key down filter event to process the CTRL + KEY and SHIFT + KEY events.
    Example attached in 7.1
    cheers
    David
    Attachments:
    Catching Alt Key Press Poll with Events(151551).vi ‏89 KB

  • Capturing Key press when the focus is not on VI. (event is not working).

    I have a VI, and found similar in Forums. I need to catch key down event, when the focus is not on VI.
    I attached the VI found in forum. Try to click on other window, for example on block diagram of VI, when VI is running and check if it works. The answer is no, it doesn't work. When you press any key, the led button must change it's state, but it does only when the focus is on VI's front panel. The question is how to control key press, when VI is minimized or inactive. Is it possible to catch key down or any similar event, when the focus is on any other application, not on VI?
    Solved!
    Go to Solution.
    Attachments:
    simulatekeyevent.vi ‏16 KB

    Have a look at the "Connectivity...input devices..." menu.
    For an example, open the example finder and look for "basic input demo". It should give you some ideas. Good luck!
    LabVIEW Champion . Do more with less code and in less time .

  • Capturing the TAB key event.

    How can I capture the TAB key event?
    I am trying to use it within a JTextField to do a normal text tab operation.
    Thanks,
    D

    I want to map the Tab key to a jTextField so that when the user is editing inside the jTextField they can press the TAB key to tab over a predefined number of characters.
    Thanks,
    D

Maybe you are looking for