gpt4 book ai didi

java - 为什么KeyAdapter无法理解shift +1 =?

转载 作者:行者123 更新时间:2023-12-01 12:59:41 24 4
gpt4 key购买 nike

到计算机:

int key = e.getKeyCode();
if(e.getKeyCode() == VK_Shift && e.getKeyCode() == VK_1){
text = "!";
}

什么也没做,但对我来说应该将其设置为感叹号。为什么这不起作用?我正在尝试制作一个游戏,在聊天框中,当用户按 Shift 1 时,我需要将文本设置为感叹号。

最佳答案

Why won't this work?

您询问单个值 (e.getKeyCode()) 是否等于 VK_ShiftVK_1。除非这两个值具有相同的值(事实上并非如此),否则 if 条件永远都不会得到满足。

文档对此说得很清楚:

Virtual key codes are used to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes (such as "A", which comes from shift and "a").

For example, pressing the Shift key will cause a KEY_PRESSED event with a VK_SHIFT keyCode, while pressing the 'a' key will result in a VK_A keyCode. After the 'a' key is released, a KEY_RELEASED event will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar value of 'A' is generated.

换句话说,您需要处理两个 KEY_PRESSED 事件,并记住第一个 (shift) 作为第二个 (1) 的上下文。

也许您应该寻找 KEY_TYPED 事件(使用 KeyAdapter.keyTyped())并使用 e.getKeyChar()

关于java - 为什么KeyAdapter无法理解shift +1 =?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23605008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com