Reputation: 13536
Is it possible to post a Key
event which contains the character as it should be received by the focused widget?
I want to make a simple pop-up keyboard which generates Key
events and I would like to make the pop-up keyboard completely independent of the physical keyboard regarding key mapping.
What I currently see is, that when I set the character to '(' without generating shift events, I get an '8' (I have a German keyboard where '(' is Shift+8). I assume that on a different keyboard the '(' will appear as something else again.
Is there any way to by-pass the mapping?
My current code:
private void postKey(Key key) {
if(shift) {
Event ke = new Event();
ke.type = SWT.KeyDown;
ke.keyCode = SWT.SHIFT;
refWidget.getDisplay().post(ke);
}
Event ke = new Event();
ke.type = SWT.KeyDown;
ke.character = shift ? key.shifted[0] : key.normal[0];
refWidget.getDisplay().post(ke);
ke.type = SWT.KeyUp;
refWidget.getDisplay().post(ke);
if(shift) {
Event ev = new Event();
ev.type = SWT.KeyUp;
ev.keyCode = SWT.SHIFT;
refWidget.getDisplay().post(ev);
}
}
Upvotes: 1
Views: 1906
Reputation: 20985
The SWTBot wiki has a page on keyboard layouts.
If you stick with Display-post()
, then you need to know the current keyboard layout and map keys to characters (shift + 8 -> '('), like you already observed.
You migh be able to re-use or adopt the relevant classes from SWTBot (SWTKeyboardStrategy
, KeyboardLayout
).
As an alternative, the page lists the AWTKeyboardStrategy that uses AWTs Robot
to simulate key strokes, that might be worth trying.
As a side note: if you had to post a Shift+x key event you would specify the Shift key in the stateMask
like so:
Event ev = new Event();
ev.type = SWT.KeyUp;
ev.stateMask = SWT.SHIFT;
ev.keyCode = ...
Upvotes: 2