[英]Double Click Function for Key Listener using Arrays and Array lists in java counts everything as a double click
The java KeyListener class has no option for a double key press, so I attempted to make my own using multiple arrays and arraylists. java KeyListener类没有双键按下的选项,所以我尝试使用多个数组和arraylists。
Here is the relevent code 这是相关代码
private ArrayList<Integer> ButtonsPressedChar = new ArrayList<Integer>();
private ArrayList<Long> ButtonsPressedTimes = new ArrayList<Long>();
private int[] PossiblePresses = new int[222];
private ArrayList<Integer> DoublePresses = new ArrayList<Integer>();
private ArrayList<Integer> ToBeRemovedChars = new ArrayList<Integer>();
private ArrayList<Integer> ToBeRemovedTimes = new ArrayList<Integer>();
private Long LastTimeForDoublePress;
private Long NowTime;
private int PositionTime;
private int PositionChar;
public void DetectDoubleKeyPresses(){
PositionTime = 0;
PositionChar = 0;
NowTime = System.currentTimeMillis();
for(int temp : PossiblePresses){
PossiblePresses[temp] = 0;
}
for(Long temp: ButtonsPressedTimes){
if( temp - NowTime >= 300){
ButtonsPressedChar.remove(PositionTime);
ToBeRemovedTimes.add(PositionTime);
System.out.println("removed");
}
PositionTime++;
}
for(int temp: ToBeRemovedTimes){
ButtonsPressedTimes.remove(temp);
System.out.println("DONE");
}
DoublePresses.clear();
for(Integer temp: ButtonsPressedChar){
System.out.println("hi 1");
if( PossiblePresses[temp] == 0){
System.out.println("First Press" + temp + PossiblePresses[temp]);
PossiblePresses[temp] = 2;
}
else if(PossiblePresses[temp] == 2){
DoublePresses.add(temp);
System.out.println(ButtonsPressedChar.size());
ToBeRemovedChars.add(temp);
ButtonsPressedTimes.remove(PositionChar);
System.out.println("DOUBLE PRESS" + temp.toString()+ PossiblePresses[temp]);
}
System.out.println("hi 2");
PositionChar++;
}
for(Integer temp: ToBeRemovedChars){
while(ButtonsPressedChar.contains(temp)){
ButtonsPressedChar.remove(temp);
}
System.out.println("DONE");
}
ToBeRemovedChars.clear();
LastTimeForDoublePress =NowTime;
}
public class Keys implements KeyListener{
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if(ButtonsHeld.contains(e.getKeyCode())){
Integer tempInteger = new Integer(e.getKeyCode());
ButtonsHeld.remove(tempInteger);
}
else
System.out.println("ERROR");
System.out.println(e.getKeyCode());
ButtonsPressedChar.add( e.getKeyCode());
ButtonsPressedTimes.add(System.currentTimeMillis())
}
@Override
public void keyPressed(KeyEvent e) {
if(ButtonsHeld.contains(e.getKeyCode())){
System.out.println("Error: Attempted Key Press Twice, original press was not removed from ButtonsHeld");
}
else if(!ButtonsHeld.contains(e.getKeyCode())){
ButtonsHeld.add(e.getKeyCode());
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public ArrayList<Integer> ButtonsHeld = new ArrayList<Integer>();
public void KeyHeldChecker(){
}
}
I'm so sorry if some of the brackets are in weird places, i had to adjust everything to get it in the code block 我很抱歉,如果某些括号位于奇怪的地方,我必须调整所有内容才能在代码块中获取它
Detect Double Key presses is executed 60 times a second. 检测双键按压每秒执行60次。 The 'hi 1' and 'hi 2' were just part of my debugging to see what was being executed.
'hi 1'和'hi 2'只是我调试的一部分,可以看到正在执行的内容。 This is what is executed when J (key code 74) is pressed a single time:
这是一次按下J(键代码74)时执行的操作:
74 hi 1 First press740 hi 2 hi 1 1 DOUBLE PRESS742 hi 2 DONE
At first i thought possible presses was not being cleared properly, but the for loop seems to be working. 起初我认为可能的印刷机没有被正确清理,但for循环似乎正在起作用。 If anyone knows why this isn't working or even just a better way of doing this that would be amazing.
如果有人知道为什么这不起作用,甚至只是一个更好的方式来做这将是惊人的。
Sorry for the bad format, I honestly can't get it to work and are not finding any quadruple spaces anywhere they shouldn't be. 抱歉格式不好,老实说,我无法让它工作,也没有找到任何不应该的四重空间。 I hope that it doesn't make it too hard to see or understand.
我希望它不会让人难以看到或理解。
If you are wanting to detect a simple double press there is no real need for any arrays or Arraylists. 如果您想要检测简单的双按,则不需要任何阵列或Arraylists。 You only need to really store the previous key press.
您只需要真正存储以前的按键。
Consider this: If you press J then K then J the J is no longer a double press so why do you care about it. 考虑一下:如果按J然后按K然后J J不再是双按,那你为什么要关心呢。
To do this you could use the following: 为此,您可以使用以下内容:
public class DoubleKeyPressDetection
{
// Below int is the number of milliseconds between the presses to determine
// if it was an intentional double press
// Adjust this for sensitivity required
private static int doublePressSpeed = 500; // Half a second
private static KeyEvent lastKeyEvent;
public static boolean isDoublePress(KeyEvent ke)
{
KeyEvent temp = lastKeyEvent;
lastKeyEvent = ke;
return temp != null
&& temp.getKeyCode() == ke.getKeyCode()
&& ((ke.getWhen() - temp.getWhen()) < doublePressSpeed);
}
}
Then in your KeyListener
然后在你的
KeyListener
@Override
public void keyPressed(KeyEvent e)
{
if(DoubleKeyPressDetection.isDoublePress(e))
System.out.println("You have double pressed " + KeyEvent.getKeyText(e.getKeyCode()));
}
Points to note: 注意事项:
keyPressed
and keyReleased
methods then it will action on every keyPress and release - so will not work correctly keyPressed
和keyReleased
方法中,那么它将对每个keyPress和release发布 - 因此无法正常工作 keyTyped
method then this might not work as the KeyEvent
does not have the keyCode keyTyped
方法,那么这可能不起作用,因为KeyEvent
没有keyCode
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.