什么是Java中表示“>”的合适按键事件?

huangapple go评论175阅读模式
英文:

What is the proper Key Event for '>' in Java?

问题

以下是翻译好的内容:

我在一个项目中被告知要特别区分按键 .,< 以及 >。我看到了一个名为 KeyEvent.VK_GREATERKeyEvent.VK_LESS 的 KeyEvent,但当我通过打印字符串进行测试时,什么都没有发生。

请帮帮我 什么是Java中表示“>”的合适按键事件?

  1. public void keyPressed(KeyEvent e)
  2. {
  3. boolean b = Utilities.isShiftDown(e);
  4. switch (e.getKeyCode())
  5. {
  6. case KeyEvent.VK_Q:
  7. System.exit(0);
  8. break;
  9. case KeyEvent.VK_LESS:
  10. System.out.println("I'm Left");
  11. break;
  12. }
  13. }
  14. }
英文:

I was told in a project to especially differentiate keypresses between . and , from < and >. I saw a KeyEvent called KeyEvent.VK_GREATER and KeyEvent.VK_LESS but when I test it out through printing a string, nothing happens.

Please help 什么是Java中表示“>”的合适按键事件?

  1. public void keyPressed(KeyEvent e)
  2. {
  3. boolean b = Utilities.isShiftDown(e);
  4. switch (e.getKeyCode())
  5. {
  6. case KeyEvent.VK_Q:
  7. System.exit(0);
  8. break;
  9. case KeyEvent.VK_LESS:
  10. System.out.println("I'm Left");
  11. break;
  12. }
  13. }
  14. }

答案1

得分: 2

代码部分不提供翻译,如下所示:

  1. Sure it works for `<` and `>`
  2. import java.awt.Dimension;
  3. import java.awt.event.KeyAdapter;
  4. import java.awt.event.KeyEvent;
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.SwingUtilities;
  8. public class KeyListenerTest extends JPanel {
  9. public static void main(String[] args) {
  10. SwingUtilities.invokeLater(() -> {
  11. JFrame frame = new JFrame("Foo");
  12. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13. frame.add(new KeyListenerTest());
  14. frame.pack();
  15. frame.setLocationRelativeTo(null);
  16. frame.setVisible(true);
  17. });
  18. }
  19. public KeyListenerTest() {
  20. setPreferredSize(new Dimension(400, 300));
  21. setFocusable(true);
  22. requestFocusInWindow();
  23. addKeyListener(new KeyAdapter() {
  24. @Override
  25. public void keyPressed(KeyEvent e) {
  26. super.keyPressed(e);
  27. System.out.printf("Key id: %d; Key char: %c; key code: %d; Modifiers ex: %d%n",
  28. e.getID(), e.getKeyChar(), e.getKeyCode(), e.getModifiersEx());
  29. }
  30. });
  31. }
  32. }
  33. those keys return
  34. Key id: 401; Key char: <; key code: 44; Modifiers ex: 64
  35. Key id: 401; Key char: >; key code: 46; Modifiers ex: 64
  36. These are the same key codes as common and period but with a non-0 modifiers ex of 64, consistent with the `SHIFT_DOWN_MASK` value for the modifierex, indicating that the shift key is pressed.
  37. And to specifically test for greater than and less than, you'd check both key code and modifiers ex:
  38. if (e.getKeyCode() == KeyEvent.VK_COMMA && (e.getModifiersEx() & KeyEvent.SHIFT_DOWN_MASK) != 0) {
  39. System.out.println("LESS-THAN pressed");
  40. }
  41. if (e.getKeyCode() == KeyEvent.VK_PERIOD && (e.getModifiersEx() & KeyEvent.SHIFT_DOWN_MASK) != 0) {
  42. System.out.println("GREATER-THAN pressed");
  43. }
  44. _______________________________________________________
  45. Key Bindings version of program:
  46. import java.awt.Color;
  47. import java.awt.Dimension;
  48. import java.awt.event.ActionEvent;
  49. import java.awt.event.KeyEvent;
  50. import javax.swing.AbstractAction;
  51. import javax.swing.ActionMap;
  52. import javax.swing.BorderFactory;
  53. import javax.swing.InputMap;
  54. import javax.swing.JFrame;
  55. import javax.swing.JPanel;
  56. import javax.swing.KeyStroke;
  57. import javax.swing.SwingUtilities;
  58. @SuppressWarnings("serial")
  59. public class KeyBindingTest extends JPanel {
  60. public static void main(String[] args) {
  61. SwingUtilities.invokeLater(() -> {
  62. JFrame frame = new JFrame("Foo");
  63. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64. frame.add(new KeyBindingTest());
  65. frame.pack();
  66. frame.setLocationRelativeTo(null);
  67. frame.setVisible(true);
  68. });
  69. }
  70. public KeyBindingTest() {
  71. setPreferredSize(new Dimension(400, 300));
  72. KeyStroke lessThanKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_COMMA, KeyEvent.SHIFT_DOWN_MASK);
  73. KeyStroke greaterThanKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, KeyEvent.SHIFT_DOWN_MASK);
  74. int condition = WHEN_IN_FOCUSED_WINDOW;
  75. InputMap inputMap = getInputMap(condition);
  76. ActionMap actionMap = getActionMap();
  77. borderFlash(lessThanKeyStroke, inputMap, actionMap, Color.BLUE);
  78. borderFlash(greaterThanKeyStroke, inputMap, actionMap, Color.RED);
  79. }
  80. private void borderFlash(KeyStroke ks, InputMap inputMap, ActionMap actionMap, Color color) {
  81. KeyStroke press = KeyStroke.getKeyStroke(ks.getKeyCode(), ks.getModifiers(), false);
  82. KeyStroke release = KeyStroke.getKeyStroke(ks.getKeyCode(), ks.getModifiers(), true);
  83. inputMap.put(press, press.toString());
  84. inputMap.put(release, release.toString());
  85. actionMap.put(press.toString(), new BorderFlashAction(color, false));
  86. actionMap.put(release.toString(), new BorderFlashAction(color, true));
  87. }
  88. private class BorderFlashAction extends AbstractAction {
  89. private static final int THICKNESS = 20;
  90. private Color color;
  91. private boolean release;
  92. public BorderFlashAction(Color color, boolean release) {
  93. this.color = color;
  94. this.release = release;
  95. }
  96. @Override
  97. public void actionPerformed(ActionEvent e) {
  98. if (release) {
  99. setBorder(null);
  100. } else {
  101. setBorder(BorderFactory.createLineBorder(color, THICKNESS));
  102. }
  103. }
  104. }
  105. }
英文:

Sure it works for &lt; and &gt;

  1. import java.awt.Dimension;
  2. import java.awt.event.KeyAdapter;
  3. import java.awt.event.KeyEvent;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.SwingUtilities;
  7. public class KeyListenerTest extends JPanel {
  8. public static void main(String[] args) {
  9. SwingUtilities.invokeLater(()-&gt; {
  10. JFrame frame = new JFrame(&quot;Foo&quot;);
  11. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. frame.add(new KeyListenerTest());
  13. frame.pack();
  14. frame.setLocationRelativeTo(null);
  15. frame.setVisible(true);
  16. });
  17. }
  18. public KeyListenerTest() {
  19. setPreferredSize(new Dimension(400, 300));
  20. setFocusable(true);
  21. requestFocusInWindow();
  22. addKeyListener(new KeyAdapter() {
  23. @Override
  24. public void keyPressed(KeyEvent e) {
  25. super.keyPressed(e);
  26. System.out.printf(&quot;Key id: %d; Key char: %c; key code: %d; Modifiers ex: %d%n&quot;,
  27. e.getID(), e.getKeyChar(), e.getKeyCode(), e.getModifiersEx());
  28. }
  29. });
  30. }
  31. }

those keys return

  1. Key id: 401; Key char: &lt;; key code: 44; Modifiers ex: 64
  2. Key id: 401; Key char: &gt;; key code: 46; Modifiers ex: 64

These are the same key codes as common and period but with a non-0 modifiers ex of 64, consistent with the SHIFT_DOWN_MASK value for the modifierex, indicating that the shift key is pressed.

And to specifically test for greater than and less than, you'd check both key code and modifiers ex:

  1. if (e.getKeyCode() == KeyEvent.VK_COMMA &amp;&amp; (e.getModifiersEx() &amp; KeyEvent.SHIFT_DOWN_MASK) != 0) {
  2. System.out.println(&quot;LESS-THAN pressed&quot;);
  3. }
  4. if (e.getKeyCode() == KeyEvent.VK_PERIOD &amp;&amp; (e.getModifiersEx() &amp; KeyEvent.SHIFT_DOWN_MASK) != 0) {
  5. System.out.println(&quot;GREATER-THAN pressed&quot;);
  6. }

Key Bindings version of program:

  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.KeyEvent;
  5. import javax.swing.AbstractAction;
  6. import javax.swing.ActionMap;
  7. import javax.swing.BorderFactory;
  8. import javax.swing.InputMap;
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11. import javax.swing.KeyStroke;
  12. import javax.swing.SwingUtilities;
  13. @SuppressWarnings(&quot;serial&quot;)
  14. public class KeyBindingTest extends JPanel {
  15. public static void main(String[] args) {
  16. SwingUtilities.invokeLater(() -&gt; {
  17. JFrame frame = new JFrame(&quot;Foo&quot;);
  18. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. frame.add(new KeyBindingTest());
  20. frame.pack();
  21. frame.setLocationRelativeTo(null);
  22. frame.setVisible(true);
  23. });
  24. }
  25. public KeyBindingTest() {
  26. setPreferredSize(new Dimension(400, 300));
  27. KeyStroke lessThanKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_COMMA, KeyEvent.SHIFT_DOWN_MASK);
  28. KeyStroke greaterThanKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, KeyEvent.SHIFT_DOWN_MASK);
  29. int condition = WHEN_IN_FOCUSED_WINDOW;
  30. InputMap inputMap = getInputMap(condition);
  31. ActionMap actionMap = getActionMap();
  32. borderFlash(lessThanKeyStroke, inputMap, actionMap, Color.BLUE);
  33. borderFlash(greaterThanKeyStroke, inputMap, actionMap, Color.RED);
  34. }
  35. private void borderFlash(KeyStroke ks, InputMap inputMap, ActionMap actionMap, Color color) {
  36. KeyStroke press = KeyStroke.getKeyStroke(ks.getKeyCode(), ks.getModifiers(), false);
  37. KeyStroke release = KeyStroke.getKeyStroke(ks.getKeyCode(), ks.getModifiers(), true);
  38. inputMap.put(press, press.toString());
  39. inputMap.put(release, release.toString());
  40. actionMap.put(press.toString(), new BorderFlashAction(color, false));
  41. actionMap.put(release.toString(), new BorderFlashAction(color, true));
  42. }
  43. private class BorderFlashAction extends AbstractAction {
  44. private static final int THICKNESS = 20;
  45. private Color color;
  46. private boolean release;
  47. public BorderFlashAction(Color color, boolean release) {
  48. this.color = color;
  49. this.release = release;
  50. }
  51. @Override
  52. public void actionPerformed(ActionEvent e) {
  53. if (release) {
  54. setBorder(null);
  55. } else {
  56. setBorder(BorderFactory.createLineBorder(color, THICKNESS));
  57. }
  58. }
  59. }
  60. }

答案2

得分: 0

也许对于一些简单的操作,可以使用 KeyEvent#getKeyChar() 方法,像这样:

  1. if (e.isShiftDown()) {
  2. switch (e.getKeyChar()) {
  3. case 'Q':
  4. System.out.println("退出!");
  5. System.exit(0);
  6. break;
  7. case '<':
  8. System.out.println("我在左边");
  9. break;
  10. }
  11. }
英文:

Well for something simple maybe use the KeyEvent#getKeyChar() method instead, like this:

  1. if (e.isShiftDown()) {
  2. switch (e.getKeyChar()) {
  3. case &#39;Q&#39;:
  4. System.out.println(&quot;EXITING!&quot;);
  5. System.exit(0);
  6. break;
  7. case &#39;&lt;&#39;:
  8. System.out.println(&quot;I&#39;m Left&quot;);
  9. break;
  10. }
  11. }

huangapple
  • 本文由 发表于 2020年4月7日 09:26:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61071408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定