如何检查 java 中单击事件时是否按下了键
How to check if key pressed when clicked event in java
我想在按下按键(不是 ctrl、shift 或 alt)时捕获 java 中的 Clicked 事件,或者检查按键 "A" 是否按下并按下按键 "D" .我该怎么做,比如 isshiftdown 方法?
像这样添加鼠标侦听器:
component.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
mouseDown = true;
}
public void mouseReleased(MouseEvent e){
mouseDown = false;
}
});
和主要听众:
component.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(mouseDown){
System.out.println("the key was pressed while the mouse is down.");
}
}
});
不确定这是否是您想要的。您可以使用 java.awt.event 包中的一些东西,称为 KeyEvent。
这是一个完整的例子:
复制到你自己的IDE并给它一个运行
我喜欢做的是有一个布尔数组来存储当前按下的键,然后在需要时使用其他方法访问它们。例如...
Boolean[] keys = new Boolean[256];
Boolean mousePressed = false;
...
keyPressed(MouseEvent e) {
keys[e.getKeyCode()] = true;
}
...
keyReleased (MouseEvent e) {
keys[e.getKeyCode()] = false;
}
....
mousePressed (MouseEvent e) { mousePressed=true; }
然后,无论何时您需要查看它是否按下了组合键,只需引用该数组并查看该键当前是否为真。它同样适用于鼠标和按键侦听器。
您可以通过创建一个布尔值数组来实现,当按下键时将该键的布尔值设置为 true,当未按下时将其设置为 false。
public boolean[] key = new boolean[68836];
不要问我为什么是68836...
您的 class 看起来像这样:
public class keyHandler implements KeyListener, MouseListener{
int KeyCode = 0;
Main main;
public KeyHandler(Main main){
this.main = main;
}
public void keyPressed(KeyEvent e) {
keyCode = e.getKeyCode();
if (keyCode > 0 && keyCode < key.length) {
main.key[keyCode] = true;
}
}
public void keyReleased(KeyEvent e) {
keyCode = e.getKeyCode();
if (keyCode > 0 && keyCode < key.length) {
main.key[keyCode] = false;
}
}
public void mousePressed(MouseEvent e) {
main.MouseButton = e.getButton();
main.MousePressed true;
}
public void mouseReleased(MouseEvent e) {
main.MouseButton = 0;
main.MousePressed = false;
}
}
接下来您将有某种循环 class 以允许您定期检查布尔值:
public class Time extends Thread{
Main main;
public Time(Main main){
this.main = main;
}
public void run(){
while(true){
main.update();// you would probably want some timing system here
}
}
}
然后在你的主要 class 或其他东西中:
public class Main {
//whatever you have here
public boolean[] key = new boolean[68836];
int MouseButton = 0;
boolean MousePressed = false;
public Main(){
Component c; //whatever you are using e.g JFrame
//whatever you do to your component
KeyHandler kh = new KeyHandler(this);
c.addKeyListener(kh);
c.addMouseListener(kh);
}
public void update(){
if(key[KeyEvent.VK_A] && key[KeyEvent.VK_D){
//both keys are pressed
}
}
}
我想在按下按键(不是 ctrl、shift 或 alt)时捕获 java 中的 Clicked 事件,或者检查按键 "A" 是否按下并按下按键 "D" .我该怎么做,比如 isshiftdown 方法?
像这样添加鼠标侦听器:
component.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
mouseDown = true;
}
public void mouseReleased(MouseEvent e){
mouseDown = false;
}
});
和主要听众:
component.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(mouseDown){
System.out.println("the key was pressed while the mouse is down.");
}
}
});
不确定这是否是您想要的。您可以使用 java.awt.event 包中的一些东西,称为 KeyEvent。
这是一个完整的例子:
复制到你自己的IDE并给它一个运行
我喜欢做的是有一个布尔数组来存储当前按下的键,然后在需要时使用其他方法访问它们。例如...
Boolean[] keys = new Boolean[256];
Boolean mousePressed = false;
...
keyPressed(MouseEvent e) {
keys[e.getKeyCode()] = true;
}
...
keyReleased (MouseEvent e) {
keys[e.getKeyCode()] = false;
}
....
mousePressed (MouseEvent e) { mousePressed=true; }
然后,无论何时您需要查看它是否按下了组合键,只需引用该数组并查看该键当前是否为真。它同样适用于鼠标和按键侦听器。
您可以通过创建一个布尔值数组来实现,当按下键时将该键的布尔值设置为 true,当未按下时将其设置为 false。
public boolean[] key = new boolean[68836];
不要问我为什么是68836... 您的 class 看起来像这样:
public class keyHandler implements KeyListener, MouseListener{
int KeyCode = 0;
Main main;
public KeyHandler(Main main){
this.main = main;
}
public void keyPressed(KeyEvent e) {
keyCode = e.getKeyCode();
if (keyCode > 0 && keyCode < key.length) {
main.key[keyCode] = true;
}
}
public void keyReleased(KeyEvent e) {
keyCode = e.getKeyCode();
if (keyCode > 0 && keyCode < key.length) {
main.key[keyCode] = false;
}
}
public void mousePressed(MouseEvent e) {
main.MouseButton = e.getButton();
main.MousePressed true;
}
public void mouseReleased(MouseEvent e) {
main.MouseButton = 0;
main.MousePressed = false;
}
}
接下来您将有某种循环 class 以允许您定期检查布尔值:
public class Time extends Thread{
Main main;
public Time(Main main){
this.main = main;
}
public void run(){
while(true){
main.update();// you would probably want some timing system here
}
}
}
然后在你的主要 class 或其他东西中:
public class Main {
//whatever you have here
public boolean[] key = new boolean[68836];
int MouseButton = 0;
boolean MousePressed = false;
public Main(){
Component c; //whatever you are using e.g JFrame
//whatever you do to your component
KeyHandler kh = new KeyHandler(this);
c.addKeyListener(kh);
c.addMouseListener(kh);
}
public void update(){
if(key[KeyEvent.VK_A] && key[KeyEvent.VK_D){
//both keys are pressed
}
}
}