問題描述
我編寫了一個 SWT UI,它的主要功能是在 StyledText 控件中顯示文本.我想為 Ctrl+F 添加一個處理程序,以便在按下該快捷方式時將焦點設置為搜索框.我嘗試使用以下代碼來檢測按鍵.
I have written an SWT UI which has a primary function of displaying text in a StyledText control. I want to add a handler for Ctrl+F so that when that shortcut is pressed the focus is set to a search box. I have tried using the following code to detect the keypress.
sWindow = new Shell();
...
sWindow.getDisplay().addFilter(SWT.KeyDown, new Listener()
{
@Override
public void handleEvent(Event e)
{
System.out.println("Filter-ctrl: " + SWT.CTRL);
System.out.println("Filter-mask: " + e.stateMask);
System.out.println("Filter-char: " + e.character);
}
});
我期待當我按下 Ctrl+f 時,我會看到以下輸出:
I was expecting that when I pressed Ctrl+f I would see the following output:
Filter-ctrl: 262144
Filter-mask: 262144
Filter-char: f
但是,在實踐中,我實際上看到了以下內容.
However, in practice I actually see the following.
Filter-ctrl: 262144
Filter-mask: 262144
Filter-char: <unprintable char - displayed as a box in eclipse console>
我有兩個問題:
- Display.addFilter(...) 是添加全局快捷方式的最佳方式嗎?我試過 Display.addListener(...) 但這根本沒有收到任何事件.
- 為什么我在按住 Ctrl 的時候沒有得到按下的字符?當我按住 alt 或 shift 時,我得到了預期的掩碼和按下的字符.
- Is Display.addFilter(...) the best way to add a global shortcut? I tried Display.addListener(...) but this didn't receive any events at all.
- Why don't I get the pressed character when I'm holding down Ctrl? When I hold down alt or shift I get the expected mask and the pressed character.
推薦答案
Display.addFilter(...) 是添加全局快捷方式的最佳方式嗎?我試過 Display.addListener(...) 但這根本沒有收到任何事件.
是的,通常 Display.addFilter(...)
是添加 glbal 快捷方式的最佳方式,因為它們比事件偵聽器具有更高的偏好.請參閱 Display.addFilter(...)
javadoc 中的以下注釋.
Yes, normally Display.addFilter(...)
is the best way to add a glbal shortcut because they have higher preference over the event listeners. See the below comment from Display.addFilter(...)
javadoc.
因為事件過濾器在其他過濾器之前運行監聽器,事件過濾器都可以阻止其他偵聽器并設置事件中的任意字段.為了因此,事件過濾器都是強大而危險.他們應該通常為了性能而避免,調試和代碼維護原因.
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.
<小時>第二個問題:
為什么我在按住 ctrl 時沒有得到按下的字符?當我按住 alt 或 shift 時,我得到了預期的掩碼和按下的字符.
問題是你看錯了地方.您應該使用 e.keyCode
而不是查詢 e.character
.根據 e.character
的 javadoc,您不會只得到字符 f
:
The problem is that you are looking at the wrong place. Instead of querying e.character
you should be using e.keyCode
. As per javadoc of e.character
you won't get just character f
:
根據事件,角色由鍵入的鍵表示.這是最后一個角色所有修飾符后的結果應用.例如,當用戶鍵入Ctrl+A,字符值為0x01 (ASCII SOH).
Depending on the event, the character represented by the key that was typed. This is the final character that results after all modifiers have been applied. For example, when the user types Ctrl+A, the character value is 0x01 (ASCII SOH).
因此,當您按下 CTRL+f 時,它會轉換為 0x06
(ASCII ACK).當您執行 ALT+f 或 SHIFT+f 時,情況并非如此.
So when you press CTRL+f then it converts in 0x06
(ASCII ACK). Which is not the case when you do ALT+f or SHIFT+f.
另一方面,e.keyCode
的 javadoc 說:
On the other hand the javadoc of e.keyCode
says:
根據事件,關鍵代碼鍵入的鍵,如定義的那樣通過類中的關鍵代碼常量SWT.當字符字段事件不明確,這個字段包含不受影響的值原始字符.例如,鍵入 Ctrl+M 或 Enter both 結果字符 ' ' 但 keyCode字段也將包含 ' ' 時當 Ctrl+M 時輸入了 Enter 和 'm'已輸入.
depending on the event, the key code of the key that was typed, as defined by the key code constants in class SWT. When the character field of the event is ambiguous, this field contains the unaffected value of the original character. For example, typing Ctrl+M or Enter both result in the character ' ' but the keyCode field will also contain ' ' when Enter was typed and 'm' when Ctrl+M was typed.
查看以下代碼了解更多詳情.對于演示,我嘗試將偵聽器放在 Display
和 Test
上.
Check the below code for more details. For demo I have tried to put listener on Display
and Test
.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ControlF
{
public static void main(String[] args)
{
Display display = new Display ();
final Shell shell = new Shell (display);
final Color green = display.getSystemColor (SWT.COLOR_GREEN);
final Color orig = shell.getBackground();
display.addFilter(SWT.KeyDown, new Listener() {
public void handleEvent(Event e) {
if(((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'f'))
{
System.out.println("From Display I am the Key down !!" + e.keyCode);
}
}
});
shell.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
if(((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'f'))
{
shell.setBackground(orig);
System.out.println("Key up !!");
}
}
public void keyPressed(KeyEvent e) {
if(((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'f'))
{
shell.setBackground(green);
System.out.println("Key down !!");
}
}
});
shell.setSize (200, 200);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
這篇關于如何在我的 SWT 應用程序中檢測 ctrl-f的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!