For the first question regarding KeyStroke thingy, I think you can use KeyBinding instead of using KeyListener, which can give you the desired result, without the problems associated with focusing the component in question, albeit in Java dimensions.
In the example below, the focus is on the JTextField first, so if you press CTRL + D , then the paintAction property attached to the CustomPanel will work, although the focus is on the JTextField .
Although if you use the setMnemonic () method for JButton , JButton will receive focus and will perform its associated action, which should draw the Ovals. This can be seen by pressing ALT + C to see the desired effect. Again, in order to accomplish things related to the drawing, both components in question do not need focus, but they still respond to KeyStrokes.
Here is a sample code:
import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class SSCCE { private final int WIDTH = 500; private final int HEIGHT = 500; private CustomPanel customPanel; private JButton circleButton; private JTextField tfield; private Random random; private int mode; private Action paintAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent ae) { mode = random.nextInt(3); Color color = new Color(random.nextFloat(), random.nextFloat() , random.nextFloat(), random.nextFloat()); customPanel.setValues(random.nextInt(WIDTH), random.nextInt(HEIGHT), random.nextInt(WIDTH), random.nextInt(HEIGHT), color, mode); } }; private ActionListener buttonAction = new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { Color color = new Color(random.nextFloat(), random.nextFloat() , random.nextFloat(), random.nextFloat()); customPanel.setValues(random.nextInt(WIDTH), random.nextInt(HEIGHT), random.nextInt(WIDTH), random.nextInt(HEIGHT), color, 2); } }; public SSCCE() { random = new Random(); } private void displayGUI() { JFrame frame = new JFrame("SSCCE"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout(5, 5)); customPanel = new CustomPanel(); customPanel.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW).put( KeyStroke.getKeyStroke(KeyEvent.VK_D , InputEvent.CTRL_DOWN_MASK), "paintAction"); customPanel.getActionMap().put("paintAction", paintAction); JPanel footerPanel = new JPanel(); circleButton = new JButton("Draw Circle"); circleButton.setMnemonic(KeyEvent.VK_C); circleButton.addActionListener(buttonAction); tfield = new JTextField(20); tfield.setText("USELESS, just to get the focus for itself."); tfield.requestFocusInWindow(); footerPanel.add(tfield); footerPanel.add(circleButton); contentPane.add(customPanel, BorderLayout.CENTER); contentPane.add(footerPanel, BorderLayout.PAGE_END); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new SSCCE().displayGUI(); } }); } } class CustomPanel extends JPanel { private final int WIDTH = 500; private final int HEIGHT = 500; private int mode = 0; private Color colorShape; private int x = 0; private int y = 0; private int width = 0; private int height = 0; public void setValues(int x, int y, int w, int h, Color color, int mode) { this.x = x; this.y = y; this.width = w; this.height = h; this.colorShape = color; this.mode = mode; repaint(); } @Override public Dimension getPreferredSize() { return (new Dimension(WIDTH, HEIGHT)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(colorShape); if (mode == 1) g.fillRect(x, y, width, height); else if (mode == 2) g.fillOval(x, y, width, height); } }
Regarding mousePressed() thingy, @mKorbel, presented all this, as usual, deliciously.
And regarding your second question, it looks like you yourself did some homework. It seems that either using what you showed in your question is a workaround for catching events related to the operating system and passing them to your Java application or Java Native Interface , I think it might work for that as well.