JAVA中的azerty键盘的键代码 [英] KeyCodes for azerty keyboard in JAVA

查看:141
本文介绍了JAVA中的azerty键盘的键代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我对Java编程还不陌生,因此我试图弄清keyEventListeners在Java中是如何工作的.我已经设法制作了一个微型程序,您可以在其中操纵红色方块,但是唯一的问题是,它只能在QWERTY键盘上工作.

So I'm fairly new to Java programming and I'm trying to figure out how keyEventListeners work in Java. I've managed to make a tiny program in which you steer a red square, but the only problem is, it only works on a QWERTY-keyboard.

以下是检查keyCode的代码:

Here's the code that checks the keyCode:

public void keyPressed(KeyEvent e){
    int c = e.getKeyCode();

    if(c == KeyEvent.VK_LEFT){
        velX = -1;
        velY = 0;
    }
    if(c == KeyEvent.VK_UP){
        velX = 0;
        velY = -1;
    }
    if(c == KeyEvent.VK_RIGHT){
        velX = 1;
        velY = 0;
    }
    if(c == KeyEvent.VK_DOWN){
        velX = 0;
        velY = 1;
    }
}

如何将其更改为azerty箭头键,以及(如果可能)将其更改为? 预先感谢.

How do I change this to azerty arrow keys, and (if possible) both? Thanks in advance.

推荐答案

您可以将无法识别的键码与键字符一起打印(对于箭头键可能不太有用).这样,您可以找出不同类型键盘上的键码是什么.

You could print the key codes you do not recognize, together with the key character (is probably not very helpful for the arrow keys). This way you could find out what the key codes on a different type of keyboard are.

箭头键在azerty键盘上是否还有其他键代码,或者这些键是否映射到相同的键代码(KeyEvent.VK_LEFT和其他键)?如果您还有其他键码,可以将其添加到if条件中.

Do the arrow keys have other key codes on an azerty keyboard or are those keys mapped to the same key codes (KeyEvent.VK_LEFT and the others)? If you have other key codes, you could add those to the if conditions.

在代码中:

public void customKeyPressed(KeyEvent e){
    int c = e.getKeyCode();

    if(c == KeyEvent.VK_LEFT || c == AZERTY_LEFT){
        velX = -1;
        velY = 0;
    } else if(c == KeyEvent.VK_UP || c == AZERTY_UP){
        velX = 0;
        velY = -1;
    } else if(c == KeyEvent.VK_RIGHT || c == AZERTY_RIGHT){
        velX = 1;
        velY = 0;
    } else if (c == KeyEvent.VK_DOWN || c == AZERTY_DOWN) {
        velX = 0;
        velY = 1;
    } else
        System.out.println(e.getKeyChar() + " = " + c);
}

这篇关于JAVA中的azerty键盘的键代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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