java键盘事件

最近看到有人问怎么模拟钢琴操作
想了想怎么实现 顺便看了下键盘事件
可以用两种方式
1、
 public class KeystrokeTest extends JApplet {
 private JButton button = new JButton("button");

 public void init() {
  Container contentPane = getContentPane();
  JPanel panel = new JPanel();
  JCheckBox checkbox = new JCheckBox("checkbox");
  JButton southButton = new JButton("south button");
  Listener listener = new Listener();

  panel.setBorder(BorderFactory
    .createTitledBorder(("Ancestor of button and checkbox")));

  checkbox.registerKeyboardAction(listener, KeyStroke.getKeyStroke(
    KeyEvent.VK_F, 0, false), JComponent.WHEN_FOCUSED);

  panel.registerKeyboardAction(listener, KeyStroke.getKeyStroke(
    KeyEvent.VK_A, InputEvent.ALT_MASK, false),
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

  southButton.registerKeyboardAction(listener, KeyStroke.getKeyStroke(
    KeyEvent.VK_W, 0, true), JComponent.WHEN_IN_FOCUSED_WINDOW);

  panel.add(button);
  panel.add(checkbox);

  contentPane.add(panel, "Center");
  contentPane.add(southButton, "South");
 }
}

class Listener implements ActionListener {
 public void actionPerformed(ActionEvent e) {
  Object src = e.getSource();
  String cname = src.getClass().getName();

  if (src instanceof JCheckBox) {
   System.out.print("'f' key PRESSED when checkbox");
   System.out.println(" had focus");
  } else if (src instanceof JPanel) {
   System.out.print("'ALT-a' key PRESSED when ancestor");
   System.out.println(" of titled panel had focus");
  } else if (src instanceof JButton) {
   System.out.print("'w' key RELEASED when any");
   System.out.println(" component in window had focus");
  }
  System.out.println("Source: " + cname);
  System.out.println();
 }
}

2、
public class T {
 public static void main(String[] args) {
  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e) {
  }

  final JTextField tf = new JTextField();
  tf.setEnabled(false);

  JPanel p = new JPanel(new GridLayout(0, 3, 5, 5));
  for (int i = 0; i <= 9; i++) {
   final JButton btn = new JButton(String.valueOf(i));
   p.add(btn);

   btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
     KeyStroke.getKeyStroke((char) (i + '0')), "PressKeyAction");
   btn.getActionMap().put("PressKeyAction", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
     btn.doClick();
    }
   });

   btn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     tf.setText(tf.getText() + btn.getText());
    }
   });
  }

  JFrame f = new JFrame();
  JComponent contentPane = (JComponent) f.getContentPane();
  contentPane.setLayout(new BorderLayout(0, 5));
  contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

  f.getContentPane().add(p, BorderLayout.CENTER);
  f.getContentPane().add(tf, BorderLayout.NORTH);
  f.pack();
  f.setLocationRelativeTo(null);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setVisible(true);
 }
}

看了下java的源代码发现registerKeyboardAction方法其实就是把
btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
     KeyStroke.getKeyStroke((char) (i + '0')), "PressKeyAction");
   btn.getActionMap().put("PressKeyAction", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
     btn.doClick();
    }
   });封装了一下


    public void registerKeyboardAction(ActionListener anAction,String aCommand,KeyStroke aKeyStroke,int aCondition) {

 InputMap inputMap = getInputMap(aCondition, true);

 if (inputMap != null) {
     ActionMap actionMap = getActionMap(true);
     ActionStandin action = new ActionStandin(anAction, aCommand);
     inputMap.put(aKeyStroke, action);
     if (actionMap != null) {
  actionMap.put(action, action);
     }
 }
    }
不过api中提到
registerKeyboardAction方法现在已过时,对于类似的操作,请结合使用 getActionMap()getInputMap(),即第二种方法为java推荐的方法
好像在哪看到过java中不提倡使用addKeyListener();
知道的可以说以下

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值