重新分配一个新的“角色”到一个键,以便打印它而不是该键的默认字母
我正在开发一个具有“宏系统”的文本编辑器,用户可以在其中将值重新分配给键盘上的按键,这样当他们按下字母 a 时,它可能会打印字母“z”反而。 (实际上,它将用于数学符号,而不是其他字母)。
谁能帮助我开始使用 Java 代码为 JTextPane 中的键重新分配值?
如果您需要更多详细信息,请告诉我。
谢谢你!
到目前为止,这就是我所拥有的:
public void keyPressed(KeyEvent evt) {
//Called when a key is pressed.
//Intercept the key before the default value is printed.
//1. Suppress the original character. Do this in the KeyEvent object
//by setting the doit property to false in your listener if the key
//matches a macro.
jTextPane1.addKeyListener(new KeyAdapter() {
public void keyPressed(keyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_A) {
//Perform an action when A is pressed and there is a macro.
if(macroA == true)
{
keyPressed.doit() = false;
}
}
}
else if (event.getKeyCode() == KeyEvent.VK_B) {
//Perform an action when B is pressed if there is a macro.
if(macroB == true)
{
keyPressed.doit() = false;
}
}
}
});
我正在研究如何通过“创建”宏来实现它,检查宏是否存在。
如果您有更多建议,我将不胜感激。
谢谢。
I'm in the process of developing a text editor that has a "macro system," where the user can reassign values to the keys on their keyboard -- so that when they press the letter a, it might print the letter "z" instead. (Really, it'll be used for math symbols, not other letters).
Can anyone get me started on the Java code to reassign a value to a key within a JTextPane?
If you need more details, let me know.
Thank you!
So far, this is what I have:
public void keyPressed(KeyEvent evt) {
//Called when a key is pressed.
//Intercept the key before the default value is printed.
//1. Suppress the original character. Do this in the KeyEvent object
//by setting the doit property to false in your listener if the key
//matches a macro.
jTextPane1.addKeyListener(new KeyAdapter() {
public void keyPressed(keyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_A) {
//Perform an action when A is pressed and there is a macro.
if(macroA == true)
{
keyPressed.doit() = false;
}
}
}
else if (event.getKeyCode() == KeyEvent.VK_B) {
//Perform an action when B is pressed if there is a macro.
if(macroB == true)
{
keyPressed.doit() = false;
}
}
}
});
I'm working on how to implement it with "creating" the macro, checking to see if a macro exists.
If you have any more advice, I would appreciate it.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一段时间没有进行任何 Swing 开发,但我认为您正在寻找 KeyListener。
这里有一个例子: http://download.oracle.com/javase/ tutorial/uiswing/events/keylistener.html
您的后端需要将按键输入与宏进行映射,并拦截按键输入并插入宏。
I haven't done any Swing development in a while, but I think you're looking for a KeyListener.
There's an example here: http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Your back end would need to map the key inputs with your macros and intercept the key input and insert the macro instead.
如果您想更改文本窗格中显示的字符,那么您有两个选择(我能想到)
a)重写在文本窗格中显示文本的代码
b) 在文档中插入不同的字符,以便绘制字符
选项二是更简单的方法。
对于简单的一对一键映射,您可以仅使用 DocumentFilter。
对于更复杂的键映射(例如使用 Ctrl+1),要输入特殊字符,您可以使用 KeyBindings。
Swing 教程 有关于这两种方法的部分。请参阅“文本组件功能”和“使用键绑定”。
If you want to change the character displayed in the text pane then you have two options (that I can think of)
a) rewrite the code that displays the text in the text pane
b) insert a different character into the Document so that character will get painted
Option two is the easier approach.
For simple one to one key mappings you can just use a DocumentFilter.
For more complex key mappings, like using Ctrl+1, to enter a special character you would then use KeyBindings.
The Swing tutorial has section on both of these approaches. See "Text Component Features" and "Using Key Bindings".