Har*_*per 2 java keyevent keylistener jtextfield
创建时KeyListener,它需要以下字段:
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
Run Code Online (Sandbox Code Playgroud)
但是,当我放入System.out.println(e)该keyPressed方法时,当按Enter键时它将返回此值:
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]
Run Code Online (Sandbox Code Playgroud)
显然这不是KeyEvent,所以我不能使用它来调用keyPressed(KeyEvent e)。我想要做的是模拟一个按键(特别是Enter键)的按下,这种方式可以激活keyListener并将该文本输出到JTextArea中。
注意:我查看了如何完美模拟KeyEvent的可接受答案 。,并且几乎不了解其实际工作原理,并且我希望我理解代码。我也在这里看过如何在Java中模拟键盘按键?,但是我不能让机器人工作;当应该按任何键时,什么也没发生。
e是KeyEvent。
如果您想查看e值,可以尝试一下
System.out.println(e.getKeyChar());
Run Code Online (Sandbox Code Playgroud)
创建KeyEvent:
KeyEvent e = new KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation);
Run Code Online (Sandbox Code Playgroud)
示例(如果这是正确的方法,则不知道,但会产生正确的输出):
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());
Run Code Online (Sandbox Code Playgroud)
这是KeyEvent参数的所有类型
java.?awt.?event.?KeyEvent
@Deprecated public KeyEvent(Component source, int id, long when, int modifiers, int keyCode)
Deprecated. as of JDK1.1
Run Code Online (Sandbox Code Playgroud)
===
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
Run Code Online (Sandbox Code Playgroud)
===
java.?awt.?event.?KeyEvent
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30588 次 |
| 最近记录: |