重新分配一个新的“字符"到键上,以便打印出来,而不是键的默认字母 [英] reassign a new "character" to a key, so that it gets printed instead of the default letter for the key

查看:69
本文介绍了重新分配一个新的“字符"到键上,以便打印出来,而不是键的默认字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有宏系统"的文本编辑器,用户可以在其中将值重新分配给键盘上的键-以便当他们按字母a时,它可以打印字母改为"z". (确实,它将用于数学符号,而不是其他字母).

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).

任何人都可以让我开始使用Java代码将值重新分配给JTextPane中的键吗?

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.

谢谢!

到目前为止,这就是我所拥有的:

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.

谢谢.

推荐答案

我已经有一段时间没有进行任何Swing开发了,但是我认为您正在寻找的是 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.

这篇关于重新分配一个新的“字符"到键上,以便打印出来,而不是键的默认字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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