【发布时间】:2014-02-27 04:10:06
【问题描述】:
我如何发送自定义 SWT 键事件,以便在不进行任何转换的情况下输入准确的文字字符?
我们正在制作一个自定义的屏幕图形键盘,它允许多种可能的布局 - QWERTY 是主要布局,但还有其他几个计划,包括视觉模拟 iPhone 键盘布局的键盘布局或其他相对于个人电脑。
我希望能够将我选择的任何任意字符作为事件发送,这样它将导致输入该确切字符。但是,每当我发送带有给定字符的事件时,SWT 似乎会根据系统的键盘设置和状态自动解释和转换它,将其更改为大写/小写或使用移位符号,具体取决于是否按下了 shift 键(例如,实际键盘上的真实键)。例如,通过键事件传递字符“T”将导致“t”或“T”,具体取决于 shift 键。它与 '4' 字符的执行类似,根据 shift 执行 '4' 或 '$'。
这是有问题的,因为我希望手动执行字符转换,并且确实可能不希望在任何给定情况下使用与标准 QWERTY 键盘相同的转换字符(例如,我希望 '$'是较小的类似手机的键盘上的“R”的移位(或“功能”键)版本)。如何使用这些事件,使其不假定使用 QWERTY 键?
SWT 事件中“字符”字段的文档说明如下:
由键入的键表示的字符。这是决赛 应用所有修饰符后产生的字符。为了 例如,当用户键入 Ctrl+A 时,字符值为 0x01。它 重要的是应用程序不要尝试修改字符 基于 stateMask(如 SWT.CTRL)或结果的值 字符将不正确。
这让我相信它不应该像现在这样转换我的角色,因为我正在命令它使用我的“最终”结果。
我创建了一个简单的独立示例来演示我的问题(请注意,这不是我实际使用键盘的方式,只是为了突出关键事件问题):
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
public class KeyboardIssueExample extends Shell {
private Text txtTextArea;
private Composite cmpButtons;
private Button btnBigA;
private Button btnBigS;
private Button btnLittleD;
private Button btnSix;
private Button btnSeven;
private Button btnDollah;
private Button btnStar;
private SelectionListener mainListener = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent selE) {
Button b = (Button)selE.widget;
char c = b.getText().charAt(0);
sendKey(c);
}
};
/**
* Launch the application.
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
KeyboardIssueExample shell = new KeyboardIssueExample(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
* @param display
*/
public KeyboardIssueExample(Display display) {
super(display, SWT.SHELL_TRIM);
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
setLayout(new GridLayout());
txtTextArea = new Text(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
txtTextArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
cmpButtons = new Composite(this, SWT.NONE);
cmpButtons.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
GridLayout gl_cmpButtons = new GridLayout(7, false);
gl_cmpButtons.marginWidth = 0;
gl_cmpButtons.marginHeight = 0;
cmpButtons.setLayout(gl_cmpButtons);
btnBigA = new Button(cmpButtons, SWT.NONE);
btnBigA.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
btnBigA.setText("A");
btnBigS = new Button(cmpButtons, SWT.NONE);
btnBigS.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
btnBigS.setText("S");
btnLittleD = new Button(cmpButtons, SWT.NONE);
btnLittleD.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
btnLittleD.setText("d");
btnSix = new Button(cmpButtons, SWT.NONE);
btnSix.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
btnSix.setText("6");
btnSeven = new Button(cmpButtons, SWT.NONE);
btnSeven.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
btnSeven.setText("7");
btnDollah = new Button(cmpButtons, SWT.NONE);
btnDollah.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
btnDollah.setText("$");
btnStar = new Button(cmpButtons, SWT.NONE);
btnStar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
btnStar.setText("*");
init();
}
private void init() {
btnBigA.addSelectionListener(mainListener);
btnBigS.addSelectionListener(mainListener);
btnLittleD.addSelectionListener(mainListener);
btnSix.addSelectionListener(mainListener);
btnSeven.addSelectionListener(mainListener);
btnDollah.addSelectionListener(mainListener);
btnStar.addSelectionListener(mainListener);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
private void sendKey(char c) {
sendKeyEvent(c, SWT.KeyDown);
sendKeyEvent(c, SWT.KeyUp);
}
private void sendKeyEvent(char c, int eventType) {
txtTextArea.forceFocus();
Event event = new Event();
event.type = eventType;
event.character = c;
getDisplay().post(event);
}
}
您会发现,关键事件不尊重我自己自定义的大小写用法,而是使用自己的大小写。好麻烦。
【问题讨论】:
-
+1 很好的问题。没想到SWT会这样……
-
@Baz 谢谢,Baz。虽然我很担心,但现在 - 你是我通常指望真正知道答案的人! =P
-
很抱歉让您失望了:/
-
@Baz Heh,对不起,如果我期望太多。我的意思是对你出色工作的赞美。
-
干杯伙伴,欣赏它;)
标签: java events keyboard swt keyevent