java - How to identify "Alt +" keypress, properly and reliably? -


My question is actually more general, but I'm using the action of the user holding the "Alt" key, And by pressing "+" as an example, which shows the difficulties.

I am working in a US English keyboard, in which "=" (lowercase) and "+" (uppercase) are on the same key, so "Alt +" (as shown in the menu entry Press), actually press "Alt Shift =" to me. In Java AWT, pressing "Alt Shift =" generates key-pressed key event with the keycode associated with the "=" key, and the key-type key event with the character "A ±". Then there is no clear, reliable way to decide "Alt" was organized when "key" was pressed.

I can do some mapping internally to fix this, like "+" to mapping the mapping "an ±" "Alt +" or "Shift {keycode for =}" though There is no guarantee that it will work in different keyboard layouts; And this is definitely not a good coding style.

If someone could suggest some way around these problems, or maybe this is a problem which has already handled this difficulty, then I would most appreciate it.

Thank you.

Try it out:

  if (e .isAltDown ()) {Switch (e.getKeyChar ()) {case '+': System.out.println ("plus"); break; }}   

Where is e keyEvent and it is pressed into key pressed Is method.

When you ALT + Shift + = on the keyboard you specify

See the following example for the complete working code:

  import java.awt.Toolkit; Import java.awt.event.KeyEvent; Import java.awt.event.KeyListener; Import javax.swing.JFrame; Import javax.swing.UIManager; Public square swing {Private stationary frames frame; Public static zero principal (string [] args throws exceptions {try} {UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ()); } Hold (exception e) {try {UIManager.setLookAndFeel (UIManager.getCrossPlatformLookAndFeelClassName ()); } Hold (Exception pre) {ex.printStackTrace (); }} Frame = new jefframe ("Event Test"); Toolkit tk = Toolkit.getDefaultToolkit (); Int xSize = ((int) tk.getScreenSize (). GetWidth () / 2) + 100; Int ySize = ((int) tk.getScreenSize (). GetHeight () / 2) + 50; Frame.setSize (xSize, ySize); Frame.setLocationRelativeTo (zero); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); (.pwk), public key (keyEvent e) {} Public Zero Key Relayed (KeyWeaver) {} Public Zero Key Pressed (KeyEVEE) {if (e.isAltDown () ) {Switch (E.K.)} {Case '+': System.out.println ("plus"); Break;}}}}); Javax.swing.SwingUtilities.invokeLater (new runnable) {Public runs zero () {frame.SsetVisible (true);}}); }}   

Hope this will help.

Comments