java 按下某个键响应_Java Swing:在按下键时执行某些操作

我通过使用

Key Bindings而不是KeyListener获得了很好的成功,并在按键时启动了

Swing Timer,然后在Swing版本上停止了Timer.您可以通过使用KeyStroke.getKeyStroke(int keyCode,int modifiers,boolean onKeyRelease)方法将正确的KeyStroke对象传递到绑定组件的

InputMap来区分按键和释放,如下所示:

KeyStroke API.如果布尔参数为false,则输入将响应按键,如果参数为真,则反转.

一个快速而不雅的例子:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.*;

@SuppressWarnings("serial")

public class KeyBindingEg extends JPanel {

private static final String UP_KEY_PRESSED = "up key pressed";

private static final String UP_KEY_RELEASED = "up key released";

private static final int UP_TIMER_DELAY = 200;

private static final Color FLASH_COLOR = Color.red;

private Timer upTimer;

private JLabel label = new JLabel();

public KeyBindingEg() {

label.setFont(label.getFont().deriveFont(Font.BOLD, 32));

label.setOpaque(true);

add(label);

setPreferredSize(new Dimension(400, 300));

int condition = WHEN_IN_FOCUSED_WINDOW;

InputMap inputMap = getInputMap(condition);

ActionMap actionMap = getActionMap();

KeyStroke upKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false);

KeyStroke upKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true);

inputMap.put(upKeyPressed, UP_KEY_PRESSED);

inputMap.put(upKeyReleased, UP_KEY_RELEASED);

actionMap.put(UP_KEY_PRESSED, new UpAction(false));

actionMap.put(UP_KEY_RELEASED, new UpAction(true));

}

private class UpAction extends AbstractAction {

private boolean onKeyRelease;

public UpAction(boolean onKeyRelease) {

this.onKeyRelease = onKeyRelease;

}

@Override

public void actionPerformed(ActionEvent evt) {

if (!onKeyRelease) {

if (upTimer != null && upTimer.isRunning()) {

return;

}

System.out.println("key pressed");

label.setText(UP_KEY_PRESSED);

upTimer = new Timer(UP_TIMER_DELAY, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

Color c = label.getBackground();

if (FLASH_COLOR.equals(c)) {

label.setBackground(null);

label.setForeground(Color.black);

} else {

label.setBackground(FLASH_COLOR);

label.setForeground(Color.white);

}

}

});

upTimer.start();

} else {

System.out.println("Key released");

if (upTimer != null && upTimer.isRunning()) {

upTimer.stop();

upTimer = null;

}

label.setText("");

}

}

}

private static void createAndShowGui() {

KeyBindingEg mainPanel = new KeyBindingEg();

JFrame frame = new JFrame("KeyBindingEg");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(mainPanel);

frame.pack();

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGui();

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值