Java swt 改变输入字符
我有 swt 浏览器小部件,用户可以在其中使用键盘输入,我需要某些字符用户按下将其更改为其他字符。 例如,当用户按 x 时,我将其更改为 y。
我添加了关键侦听器,可以使用 doit = false; 阻止用户输入。 但现在我无法传递我的性格。
这就是我正在做的事情:
browser_1.addListener(SWT.KeyDown, new Listener() {
public void handleEvent(Event arg0) {
if(arg0.character=='x')
{
arg0.doit=false;
//now here how to send y as a charachter to browser widget
}
}
});
换句话说,我可以在不使用 arg0.doit=false 的情况下以某种方式将字符更改为其他字符吗?
I have swt browser widget in which user can type with keyboard, I need for certain character user press change it to others.
for example when user press x, I change it y.
I add key listener where I can block user input with doit = false;
but now I can't pass my character.
here is what I am doing:
browser_1.addListener(SWT.KeyDown, new Listener() {
public void handleEvent(Event arg0) {
if(arg0.character=='x')
{
arg0.doit=false;
//now here how to send y as a charachter to browser widget
}
}
});
In other words can I somehow change character to other without using arg0.doit=false;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,经过一番搜索,这里是解决方案
在 SWT 中,您可以添加显示“过滤器”侦听器实例,该实例可以修改事件中的几乎所有内容(请参阅 详细信息文档)。
Javadoc 的警告: 在handleEvent() 方法中将事件类型设置为 SWT.None 可用于更改事件类型并停止后续 Java 侦听器的运行。由于事件过滤器在其他侦听器之前运行,因此事件过滤器既可以阻止其他侦听器,也可以在事件中设置任意字段。因此,事件过滤器既强大又危险。出于性能、调试和代码维护的原因,通常应该避免使用它们。
这是代码(将任何键入的键更改为
'l'
字符,并在事件实际发生时将其写入控制台)恕我直言,这确实是一个肮脏的解决方案,浏览器端的实现(通过 JavaScript)要漂亮得多
此外,当我查看您的代码时(不知道这是否只是一些测试,证明-概念代码,无论如何),使用带有
something_number
或arg0
的变量让我很难过。它使代码变得难以阅读和晦涩难懂,请尽量避免使用它们;]..So after some search, here is the solution
In SWT you can add to display 'filter' listener instance which can modify pretty anything in the event (see docs for details).
Caution from Javadoc: Setting the type of an event to SWT.None from within the handleEvent() method can be used to change the event type and stop subsequent Java listeners from running. Because event filters run before other listeners, event filters can both block other listeners and set arbitrary fields within an event. For this reason, event filters are both powerful and dangerous. They should generally be avoided for performance, debugging and code maintenance reasons.
Here's the code (changes any typed key to
'l'
character and wrote that in console, when the event actually arise)IMHO it's really dirty solution, implementation on browser side (by JavaScript) is much more prettier
Also when I'm looking to your code (don't know if it's just some testing, proof-of-concept code, anyway), using variables with
something_number
orarg0
makes me sad. It makes code so much unreadable and obscure, try to avoid them ;]..您可以执行以下操作:
对此代码的一些注释:
我们检查 e.stateMask 因为我们仍然需要将 CTRL+X 保留为剪切功能。请注意,如果您使用此代码而不是当前代码(只需检查是否按下了特殊按钮):
if (e.character == 'x' && e.stateMask == 0)
当 CapsLock 打开时,您会遇到错误。在这种情况下,用户应该按 Shift+X 来降低 x。
方法 insert("y") 将字符插入到光标位置。当选择某些文本时,整个选择将被“y”替换。
当前示例仅更改小写的“x”。如果需要处理,您应该更改它,并将大写的 X 更改为 Y。
You can do following:
Some comments to this code:
We check for e.stateMask because we still need keep CTRL+X as cut-function. Please note, if you use instead of current code this one (that just check that no special button is pressed):
if (e.character == 'x' && e.stateMask == 0)
You will get a bug when CapsLock is on. In this case user should press Shift+X to get lower x.
Method insert("y") inserts charater to the place of cursor. When some text is seleted, the whole selection will be replaced by "y".
Current example changes only lower case of "x". You should change it if needed to handle also change upper X to Y.
我有类似的要求:将键盘上的小数分隔符(点)转换为我们区域设置的小数分隔符(逗号)。我尝试了与Sorceror相同的想法,但它对我来说也不起作用。有效的方法是设置 event.doit = false 并发布一个新事件,该新事件是原始事件的克隆,并替换了字符:(
如果 display 是局部变量,则需要将其设为最终变量。)
我创建了一个小实用方法来创建事件的克隆:
I had a similar requirement: converting a decimal separator press on the keypad (a dot) to the decimal separator of our locale (a comma). I tried the same idea as Sorceror, but it didn't work for me either. What does work is setting event.doit = false and posting a new event that is the clone of the original event with the character replaced:
(If display is a local variable you need to make it final.)
I created a small utility method to create a clone of the event: