怎么处理同时按键盘事件 250
//怎么控制圆点和方框可以一起动???之前试过if(1&&2)这种根本不行packagetest1;importjava.awt.*;importjava.awt.eve...
//怎么控制圆点和方框可以一起动???之前试过 if(1&&2) 这种根本不行package test1;import java.awt.*;import java.awt.event.*;import java.util.Random;import javax.swing.*;public class Test1 extends JFrame{ public static void main(String[] args){ new Test1(); } public Test1(){ Panel p=new Panel(); add(p); Thread t=new Thread(p); t.start(); addKeyListener(p); setSize(600,600); setDefaultCloseOperation(3); setLocationRelativeTo(null); setVisible(true); }}class Panel extends JPanel implements KeyListener,Runnable{ int x=50; int y=50; int i=100; int j=100; boolean live=true; public void paint(Graphics g){ super.paint(g); if(live){ g.fillOval(x,y,10,10); g.drawRect(i,j,100,100); } if(x>i&&x<i+100 && y>j&&y<j+100){ //不圆点在方框内 live=false; }else{ //小圆点出方框 live=true; } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_UP){y-=3;} //小圆点 if(e.getKeyCode()==KeyEvent.VK_LEFT)x-=3; if(e.getKeyCode()==KeyEvent.VK_DOWN)y+=3; if(e.getKeyCode()==KeyEvent.VK_RIGHT)x+=3; if(e.getKeyCode()==KeyEvent.VK_W)j-=3; //方框 if(e.getKeyCode()==KeyEvent.VK_A)i-=3; if(e.getKeyCode()==KeyEvent.VK_S)j+=3; if(e.getKeyCode()==KeyEvent.VK_D)i+=3; if(e.getKeyCode()==KeyEvent.VK_SPACE){ //空格变色 Random r=new Random(); int n1=r.nextInt(255); int n2=r.nextInt(255); int n3=r.nextInt(255); setBackground(new Color(n1,n2,n3)); } repaint(); } public void run(){ while(true){ try { Thread.sleep(20); repaint(); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }}
展开
2个回答
展开全部
帮你查了一下api,其中有段话可能你没注意看:
the button mask returned by inputevent.getmodifiers() reflects only the button that changed state, not the current state of all buttons. (note: due to overlap in the values of alt_mask/button2_mask and meta_mask/button3_mask, this is not always true for mouse events involving modifier keys). to get the state of all buttons and modifier keys, use inputevent.getmodifiersex().
inputevent.getmodifiers()只能反射单个键的更改状态,如果想获得组合键的状态,必须用inputevent.getmodifiersex().
下面的代码我测试过,可以判断:
gtext.addmouselistener(new mouseadapter()
{
public void mousepressed(mouseevent e)
{
if(e.getbutton()==e.button1 )
glabel.settext("button1"); //判断左键
if( e.getbutton()==e.button3)
glabel.settext("button3");//判断右键
if(e.getmodifiersex()==(e.button3_down_mask + e.button1_down_mask))
glabel.settext("bt1+bt3!"); //判断左右键同时按下
if(e.getmodifiersex()==(e.shift_down_mask + e.button1_down_mask))
glabel.settext("shift+bt1");// shift+左键
//...............其他雷同,修改常量值就可以
}
}
);
或者使用 JNI/JNA 去使用 GetKeyState 检测
the button mask returned by inputevent.getmodifiers() reflects only the button that changed state, not the current state of all buttons. (note: due to overlap in the values of alt_mask/button2_mask and meta_mask/button3_mask, this is not always true for mouse events involving modifier keys). to get the state of all buttons and modifier keys, use inputevent.getmodifiersex().
inputevent.getmodifiers()只能反射单个键的更改状态,如果想获得组合键的状态,必须用inputevent.getmodifiersex().
下面的代码我测试过,可以判断:
gtext.addmouselistener(new mouseadapter()
{
public void mousepressed(mouseevent e)
{
if(e.getbutton()==e.button1 )
glabel.settext("button1"); //判断左键
if( e.getbutton()==e.button3)
glabel.settext("button3");//判断右键
if(e.getmodifiersex()==(e.button3_down_mask + e.button1_down_mask))
glabel.settext("bt1+bt3!"); //判断左右键同时按下
if(e.getmodifiersex()==(e.shift_down_mask + e.button1_down_mask))
glabel.settext("shift+bt1");// shift+左键
//...............其他雷同,修改常量值就可以
}
}
);
或者使用 JNI/JNA 去使用 GetKeyState 检测
追问
这个我搜到过,具体怎么用?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询