F10 blocks other action keys

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1627 Views - Last Post: 26 February 2012 - 06:55 PM Rate Topic: -----

#1 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

F10 blocks other action keys

Posted 25 February 2012 - 04:41 PM

So this one bothers me a lot!

I add a KeyListener on my JPanel (really this is for any component), but whenever I press the F10 action key, it blocks every other action key until I either press F10 again or another key or mouse click.

I want to turn this behaviour off, but I have no yet found a way. I tried to override the processEvent and processKeyEvent methods which handle the event before the listeners, but without any luck.

What I really want is full control of what happens when a key is pressed and released!

What I also notice is that when the F-keys are not blocked, and I press F10, it will send a key pressed and key release event, BUT when it is in a blocked state and I press F10, it will only send a key released event.

This is the code I tried testing with
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class MyFrame extends JFrame {

	public static void main(String[] args) {
		new MyFrame();
	}
	
	public MyFrame() {
		MyCanvas c = new MyCanvas();
		c.setFocusTraversalKeysEnabled(false);
		setVisible(true);
		setSize(200, 200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		add(c, BorderLayout.CENTER);
		c.requestFocus();
	}

	private class MyCanvas extends JPanel implements KeyListener {
		
		public MyCanvas() {
			super();
			setFocusTraversalKeysEnabled(false);
			setFocusable(true);
			addKeyListener(this);
		}

		
		@Override
		protected void processKeyEvent(KeyEvent e) {
			super.processKeyEvent(e);
		}
		
		//Handles the events. Delegates the events to the correct process Event methods
		@Override
		protected void processEvent(AWTEvent e) {
			super.processEvent(e);
			System.out.println("Process event: " + e.getID());
		}


		@Override
		public void keyPressed(KeyEvent e) {
			System.out.println("key pressed");
		}


		@Override
		public void keyReleased(KeyEvent e) {
		}


		@Override
		public void keyTyped(KeyEvent e) {
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: F10 blocks other action keys

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: F10 blocks other action keys

Posted 25 February 2012 - 05:07 PM

I just tried and add an AWTEventListener to the default toolkit, but even there it blocks
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: F10 blocks other action keys

Posted 25 February 2012 - 07:44 PM

Update: I think it is because F10 is supposed to focus the menu bar, so that's what it is trying to do? How can I come around this =/
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: F10 blocks other action keys

Posted 26 February 2012 - 05:50 AM

So I have found a solution!

Apparently the KeyboardFocusManager refuses to dispatch the actionkey KeyEvents after F10 is pressed, so I made my own KeyboardFocusManager implementation :)

import java.awt.DefaultKeyboardFocusManager;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;


public class MyKeyboardManager extends DefaultKeyboardFocusManager {
	private AWTEventListener listener;
	
	public MyKeyboardManager(AWTEventListener listener) {
		this.listener = listener;
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent e) {
		if (e.getID() == KeyEvent.KEY_PRESSED || e.getID() == KeyEvent.KEY_RELEASED || e.getID() == KeyEvent.KEY_TYPED) {
			listener.eventDispatched(e);
			return true;
		}
		return super.dispatchKeyEvent(e);
	}
}



Edit: I wrote return super.dispatchEvent(e); instead of return super.dispatchKeyEvent(e);

This post has been edited by CasiOo: 26 February 2012 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: F10 blocks other action keys

Posted 26 February 2012 - 10:50 AM

Quote

whenever I press the F10 action key, it blocks every other action key


What are you doing when F10 is pressed? Sounds like you're blocking the EDT
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: F10 blocks other action keys

Posted 26 February 2012 - 12:12 PM

View Postg00se, on 26 February 2012 - 10:50 AM, said:

Quote

whenever I press the F10 action key, it blocks every other action key


What are you doing when F10 is pressed? Sounds like you're blocking the EDT


Maybe I should have wrote press and release, since that is what im actually doing ^^

As you can see in my example, I only have a JFrame and an ActionListener. I click the frame to make it get focus, press and release F10, and then try and press and release any other F-key. The other F-keys do not respond until either F10 is pressed and released again or any other event (including other keys) happen.

I think that it is normal behavior in java applications that F10 focuses the menubar, and then you can use the other F-keys to navigate? But my frame doesn't have a menubar.. It looks like it is still consuming the event somehow before sending it to my actionlistener.

The following events happen:
F10 pressed and released:
Key_Pressed event (on F10)
Key_Released event (on F10)

F5 pressed and released:
No event

F10 pressed and released:
Key_Released event (on F10)

This post has been edited by CasiOo: 26 February 2012 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: F10 blocks other action keys

Posted 26 February 2012 - 12:22 PM

Is it conceivable it's being intercepted/interfered with by your windowing system?
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: F10 blocks other action keys

Posted 26 February 2012 - 12:29 PM

The f-key events get to the dispatchEvent(AWTEvent e) in the KeyboardFocusManager. But from there I dont know what makes the events get ignored or consumed.

I tried to get my friend to try and make a frame and see if it behaved the same way, and it did.. So it should hopefully do the same if you try it out.

This post has been edited by CasiOo: 26 February 2012 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: F10 blocks other action keys

Posted 26 February 2012 - 12:55 PM

It seems that you've solved this now. Out of interest i would usually bind a KeyStroke when treating F10

fyi your original code works fine on my windowing system

This post has been edited by g00se: 26 February 2012 - 01:05 PM

Was This Post Helpful? 1
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: F10 blocks other action keys

Posted 26 February 2012 - 01:09 PM

I don't know if it has anything to do with us (my friend and I) using danish keyboards or windows 7?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: F10 blocks other action keys

Posted 26 February 2012 - 01:10 PM

For the F keys, for them not to be catch by Windows you have to do something like this

	private void defineHotKeys() {

		InputMap iMap = mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		defineHotKeys(iMap);
		
		ActionMap aMap = mainPanel.getActionMap();
		aMap.put("F1pressed", new PFAction(0));
		aMap.put("F2pressed", new PFAction(1));
		...
	}
	private void defineHotKeys(InputMap iMap) {
		iMap.put(KeyStroke.getKeyStroke("ctrl F1"),  "F1pressed");
		iMap.put(KeyStroke.getKeyStroke("ctrl F2"),  "F2pressed");
		...
	}
	private class PFAction extends AbstractAction
	{
		private int n;	
		PFAction(int n) {
			this.n = n;
		}
		
		public void actionPerformed(ActionEvent e)
                  ... your code here using n= 0 > F1 was hit, n==1 > F2 was hit....
		}
	}


Was This Post Helpful? 2
  • +
  • -

#12 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: F10 blocks other action keys

Posted 26 February 2012 - 01:33 PM

That did it pbl!!

I can't thank you enough
Was This Post Helpful? 0
  • +
  • -

#13 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: F10 blocks other action keys

Posted 26 February 2012 - 02:41 PM

Sorry for asking more questions, but what if I want to do it for a Canvas which doesn't extend from JComponent and therefore doesn't have an inputmap ?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: F10 blocks other action keys

Posted 26 February 2012 - 03:48 PM

View PostCasiOo, on 26 February 2012 - 03:33 PM, said:

That did it pbl!!

I can't thank you enough

Glad I could help
So it work or not ?
Canvas are really lower level I am afraid it won't work, replace your Canvas by JPanel
or apply these methods to the JFrame to which you give the focus, in the JFrame listener call your method FKeyHit() of the class you wrote that extends Canvas
Was This Post Helpful? 0
  • +
  • -

#15 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: F10 blocks other action keys

Posted 26 February 2012 - 04:09 PM

View Postpbl, on 26 February 2012 - 03:48 PM, said:

View PostCasiOo, on 26 February 2012 - 03:33 PM, said:

That did it pbl!!

I can't thank you enough

Glad I could help
So it work or not ?
Canvas are really lower level I am afraid it won't work, replace your Canvas by JPanel
or apply these methods to the JFrame to which you give the focus, in the JFrame listener call your method FKeyHit() of the class you wrote that extends Canvas


Yes it works perfectly on JPanel :) But I did not expect it to make the actionPerformed and then notifying the keylisteners afterwards. I expected it to only to execute the action that I had set ^^

I don't see JFrame having a getInputMap or actionmap either. If that was what you meant?


The code I used to test it with. You can see it calling the actionperformed AND the keylisteners
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class MyFramePbl extends JFrame {
	private JPanel panel;
	
	public static void main(String[] args) {
		new MyFramePbl();
	}

	public MyFramePbl() {
		setFocusable(false);
		setVisible(true);
		setSize(500, 500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		panel = new MyPanel();
		add(panel, BorderLayout.CENTER);

		defineHotKeys();
		defineHotKeys(panel.getInputMap() );
		panel.requestFocus();
		
	}

	private void defineHotKeys() {
		AbstractAction dummyAction = new DummyAction();
		
		ActionMap aMap = panel.getActionMap();
		aMap.put("F1pressed", new PFAction(0)); //Do this action when F1Pressed in called
		aMap.put("F2pressed", new PFAction(1));
		
		aMap.put("dummy", dummyAction);
	}

	private void defineHotKeys(InputMap iMap) {
//		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1pressed");
//		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "F2pressed");
		//		...
		
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0), "dummy");
		iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0), "dummy");
	}
	
	private class DummyAction extends AbstractAction {
		@Override
		public void actionPerformed(ActionEvent e) {
			//Do nothing
		}
	}

	private class PFAction extends AbstractAction {
		private int n;

		PFAction(int n) {
			this.n = n;
		}

		public void actionPerformed(ActionEvent e) {
			//	                 ... your code here using n= 0 > F1 was hit, n==1 > F2 was hit....
		}
	}

	private class MyPanel extends JPanel implements KeyListener {

		public MyPanel() {
			setFocusable(true);
			addKeyListener(this);
		}

		@Override
		public void keyPressed(KeyEvent arg0) {
			System.out.println("pressed " + arg0.getKeyCode());
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			System.out.println("released " + arg0.getKeyCode());
		}

		@Override
		public void keyTyped(KeyEvent arg0) {
		}
	}
}


This post has been edited by CasiOo: 26 February 2012 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2