- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我浏览了有关如何使用键绑定(bind)的教程,并且我以前使用过它们,但现在情况不同了。我有一个钢琴卷轴,其琴键与键盘琴键相对应。我需要使用 keyPressed 和 keyReleased 方法让程序知道何时停止和开始钢琴音符。
编辑:
这是得到答案后的工作代码:
在 JLayeredPane 构造函数中:
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
mapKeyboard(im, am);
以及相应 keyPressed/keyReleased 操作的方法和类:
public void mapKeyboard(InputMap im, ActionMap am)
{
int count = 0;
for(int j = 0; j<10; j++)
{
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, false), "KeyDown" + count + "");
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, true), "KeyUp" + count + "");
am.put("KeyDown" + count + "", new WhiteKeyDown(count, j));
am.put("KeyUp" + count + "", new WhiteKeyUp(count, j));
count++;
}
for(int j = 0; j<7; j++)
{
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, false), "KeyDown" + count + "");
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, true), "KeyUp" + count + "");
am.put("KeyDown" + count + "", new BlackKeyDown(count, j));
am.put("KeyUp" + count + "", new BlackKeyUp(count, j));
count++;
}
for(int j = 10; j<17; j++)
{
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, false), "KeyDown" + count + "");
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, true), "KeyUp" + count + "");
am.put("KeyDown" + count + "", new WhiteKeyDown(count, j));
am.put("KeyUp" + count + "", new WhiteKeyUp(count, j));
count++;
}
for(int j = 7; j<12; j++)
{
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, false), "KeyDown" + count + "");
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, true), "KeyUp" + count + "");
am.put("KeyDown" + count + "", new BlackKeyDown(count, j));
am.put("KeyUp" + count + "", new BlackKeyUp(count, j));
count++;
}
}
class WhiteKeyDown extends AbstractAction
{
int index;
public WhiteKeyDown(int i, int j)
{
super("KeyDown" + i + "");
index = j;
putValue(Action.NAME, "KeyDown" + i + "");
putValue(ACTION_COMMAND_KEY, "KeyDown" + i + "");
}
@Override
public void actionPerformed(ActionEvent ke) {
if(isWhiteDown[index] == false)
{
channel.noteOn (((WhiteKey) WhiteKeys[index]).getNote (), 127);
isWhiteDown[index] = true;
WhiteKeys[index].setBackground(Color.LIGHT_GRAY);
Key key = (Key) WhiteKeys[index];
CreateOnEvent(key);
}
}
}
class WhiteKeyUp extends AbstractAction
{
int index;
public WhiteKeyUp(int i, int j)
{
super("KeyUp" + i + "");
index = j;
putValue(Action.NAME, "KeyUp" + i + "");
putValue(ACTION_COMMAND_KEY, "KeyUp" + i + "");
}
@Override
public void actionPerformed(ActionEvent ke) {
if(isWhiteDown[index] == true)
{
channel.noteOff (((WhiteKey) WhiteKeys[index]).getNote (), 127);
isWhiteDown[index] = false;
WhiteKeys[index].setBackground(Color.WHITE);
Key key = (Key) WhiteKeys[index];
CreateOffEvent(key);
}
}
}
class BlackKeyDown extends AbstractAction
{
int index;
public BlackKeyDown(int i, int j)
{
super("KeyDown" + i + "");
index = j;
putValue(Action.NAME, "KeyDown" + i + "");
putValue(ACTION_COMMAND_KEY, "KeyDown" + i + "");
}
@Override
public void actionPerformed(ActionEvent ke) {
if(isBlackDown[index] == false)
{
channel.noteOn (((BlackKey) BlackKeys[index]).getNote (), 127);
isBlackDown[index] = true;
BlackKeys[index].setBackground(Color.DARK_GRAY);
Key key = (Key) BlackKeys[index];
CreateOnEvent(key);
}
}
}
class BlackKeyUp extends AbstractAction
{
int index;
public BlackKeyUp(int i, int j)
{
super("KeyUp" + i + "");
index = j;
putValue(Action.NAME, "KeyUp" + i + "");
putValue(ACTION_COMMAND_KEY, "KeyUp" + i + "");
}
@Override
public void actionPerformed(ActionEvent ke) {
if(isBlackDown[index] == true)
{
channel.noteOff (((BlackKey) BlackKeys[index]).getNote (), 127);
isBlackDown[index] = false;
BlackKeys[index].setBackground(Color.BLACK);
Key key = (Key) BlackKeys[index];
CreateOffEvent(key);
}
}
}
最佳答案
关于java - Keybinders 可以实现 keyPressed 和 keyReleased 方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16247504/
用户特定的 keyBindings .dict 文件位于:~/Library/KeyBindings/ 您(作为开发人员)如何告诉 AppKit 在另一个目录(特定于应用程序)中查找特定应用程序的另一
我想创建一个 StaticResource KeyBinding,但我不知道如何取消引用它。因此,在我的 Window.Resources 中,我可能会添加以下内容:
好的,这是代码 Gui.java package mainProgram; import javax.swing.*; import java.awt.*;
我需要 issgin CTRL+F12 和 CTRL+G 此代码不起作用 但是这段代码有效 为什么不工作? 如何分配 CTRL+F12 和 CTRL+G? 最佳答案 使用代码设置绑
这段代码适合多次按下箭头键吗?现在正在工作。向右走,向左走。但是,如果我同时按下向左和向右箭头键,我希望该桨停止。我怎样才能做到这一点 ? import java.awt.*; import java
在我的表单中,当我按下键盘上的 ENTER 按钮时,应该调用 okAction() 方法(并且调用完美)。 我的问题是处于焦点状态,当我填写文本字段然后按下 ENTER 按钮时,okAction()
简短版本:如何通过与 KeyListener keyReleased() 事件类似的功能来确定 KeyBind 键何时被释放? 长版:我正在尝试制作一个非常简单的游戏,并且我使用了几个 KeyList
我不太确定如何用这个问题的标题来表达。基本上,我正在使用 JFrames 制作太空入侵者,并使用 KeyBindings 进行键盘输入。键绑定(bind)工作正常,我可以毫无问题地移动飞船。但是,如果
Python-keybinder 用于在我的程序中设置全局热键。 GtkToggleButton 小部件用于从用户检索键绑定(bind)(按键事件/按键释放事件)。 用户按下切换按钮并按下一些键(左
我正在尝试从 Java KeyListeners 转移到 KeyBindings 来进行动画处理,但它们会工作几秒钟,然后完全停止。当 Action 触发时,我将消息打印到控制台,并且这些消息停止,因
我正在努力创建一种识别组合键的方法。在我设置任何 boolean 值之前,我注意到某些组合键会导致忽略其他键。 package question; import java.awt.BorderLayo
无论出于何种原因,一旦我的 WPF 应用程序加载,我的 UserControl 的 KeyBindings 就无法工作。在我按下表单上的按钮后,它们会起作用,但当我通过单击或 alt 选项卡或移动或类
我想将键绑定(bind)添加到我的 JFileChooser以便在按下 SPACE 键时打开文件预览窗口。 由于源代码太大,我只是做了一个简单的脏代码: MainWindow.java package
我有一个 Jpanel (p)、一个 Jframe (f) 和这个标签 (cursorlbl),我想通过箭头键四处移动。我已经在 goLeft() 方法中做了我认为合理的事情......但它不起作用。
在我的程序中,由使用 KeyBindings 的监听器操作的 JPanel 组件(在阅读有关聚焦问题后已经从 KeyListeners 更改)被添加到 JFrame 中。这个应用程序的任务是简单地使用
我正在尝试制作一个简单的游戏,我需要我的宇宙飞船在其中心旋转(使用 A 和 D 键),并相对于它所面对的方向移动(W 和 S 键)。 我已经基本弄清楚了移动和旋转的数学原理,但我对关键听众有疑问。 当
因此,如果我在 Tmux 中并且有一堆输出到达终端,我可以按以下键滚动浏览它: ctrl-b [ 现在,我必须举起双手并使用箭头键向上滚动。 如何在滚动模式下映射 vim 键? 最佳答案 2020年更
我读到使用 KeyBindings 比使用 KeyListeners 更好。我看到了 KeyBindings 如何对特定键的特定 react 有用;但我也在尝试检测键盘上任何键的按下/释放:有没有办法
我对 Swing 的使用并不陌生,但我用它做了很多工作,并且我正在设计一个新的应用程序。这是一个绘图应用程序。它将允许用户单击空白的白色“查看器”窗口中的某些位置,使用键盘键入字母和符号,从而编辑查看
我有一个程序,我想使用键绑定(bind): // Imports.. public class Test{ JButton button = new JButton(); Test(){ //...
我是一名优秀的程序员,十分优秀!