如何创建KeyEvent [英] How to create a KeyEvent

查看:158
本文介绍了如何创建KeyEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建KeyListener时,它需要以下字段:

When i create the KeyListener, it requires the following fields:

public void keyPressed(KeyEvent e) 
{

}
public void keyReleased(KeyEvent e) 
{

}
public void keyTyped(KeyEvent e) 
{

}

但是,当我将System.out.println(e)放入keyPressed方法时,当我按Enter键时,它将返回此值:

When i put System.out.println(e) into the keyPressed method, though, it returns this when i press the enter key:

java.awt.event.KeyEvent[KEY_PRESSED,keyCode=10,keyText=?,keyChar=?,keyLocation=KEY_LOCATION_STANDARD,rawCode=0,primaryLevelUnicode=0,scancode=0] on javax.swing.JButton[,1,1,100x100,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@13b33a0e,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=HI,defaultCapable=true]

这显然不是KeyEvent,所以我不能用它来调用keyPressed(KeyEvent e).我要能够做的是模拟按键的按下,特别是Enter键,其方式是激活keyListener并将该文本输出到JTextArea中.

This is obviously not a KeyEvent, so I cannot use it to call the keyPressed(KeyEvent e). What I want to be able to do is simulate the pressing of a key, specifically the enter key, in a way that would activate the keyListener and would output that text into a JTextArea.

注意:我查看了如何完全模拟KeyEvent的可接受答案?,并且几乎不了解它的实际工作原理,我希望我能理解代码.我也在这里如何在Java中模拟键盘按键?不是,我无法让机器人工作;当应该按任何键时,什么也没发生.

Note: I looked at the accepted answer for How can I perfectly simulate KeyEvents?, and understood little of how it actually works, and i want code i understand. I also looked here How to simulate keyboard presses in java?, but not i could not get the robot to work; nothing happened when a key was supposed to be pressed.

推荐答案

e是KeyEvent.

e is the KeyEvent.

如果您想查看e值,可以尝试一下

if you want to see the e value, then you can try this

System.out.println(e.getKeyChar());

创建KeyEvent:

Creating KeyEvent :

KeyEvent e = new KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation);

示例(如果这是正确的方法,则不知道,但是会产生正确的输出):

Example (dunno if this is the right way, but it produce the right output):

Button a = new Button("click");
    KeyEvent e;
    e = new KeyEvent(a, 1, 20, 1, 10, 'a');
    System.out.println(""+e.getKeyChar());
    System.out.println(""+e.getKeyCode());

这是所有类型的KeyEvent参数

Here is the all type of KeyEvent parameters

java.​awt.​event.​KeyEvent
@Deprecated public KeyEvent(Component source, int id, long when, int modifiers, int keyCode)
Deprecated. as of JDK1.1

===

java.​awt.​event.​KeyEvent
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar)
Constructs a KeyEvent object.
Note that passing in an invalid id results in unspecified behavior. This method throws an IllegalArgumentException if source is null.
Parameters:
source - the Component that originated the event id - an integer identifying the type of event when - a long integer that specifies the time the event occurred modifiers - the modifier keys down during event (shift, ctrl, alt, meta) Either extended _DOWN_MASK or old _MASK modifiers should be used, but both models should not be mixed in one event. Use of the extended modifiers is preferred. keyCode - the integer code for an actual key, or VK_UNDEFINED (for a key-typed event) keyChar - the Unicode character generated by this event, or CHAR_UNDEFINED (for key-pressed and key-released events which do not map to a valid Unicode character) 
Throws:
IllegalArgumentException - if id is KEY_TYPED and keyChar is CHAR_UNDEFINED; or if id is KEY_TYPED and keyCode is not VK_UNDEFINED IllegalArgumentException - if source is null

===

java.​awt.​event.​KeyEvent
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation)

这篇关于如何创建KeyEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆