Is there any key binding for press CTRL and Mouse click together? - eclipse-rcp

I have editor.Now I want to enable key binding while press CTRL and Mouse click. Is there any key binding Sequence available in Eclipse RCP.

Mouse clicks don't generate key events so you can use key bindings for this.
If you have a SWT mouse event listener you can test the state of the 'modifier' keys in the listener by checking the stateMask in the MouseEvent.
For example:
#Override
public void mouseDown(MouseEvent event) {
boolean isMod1 = (event.stateMask & SWT.MOD1) != 0;
...
}
SWT.MOD1 is Ctrl on most platforms ('Command' on macOS).

Related

Detect CTRL+Click on SWT ToolItem

Is there a way to detect a CTRL-click on a ToolItem? I want to distinguish between CTRL+Click and normal mouse click.
ToolBar toolbar= new ToolBar(parent, SWT.NONE);
ToolItem saveToolItem = new ToolItem(toolbar, SWT.PUSH);
...
saveToolItem.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
// if CTRL+Click {
// specialSave();
// } else
normalSave();
}));
The SelectionEvent passed to the event (in e in your code) has a stateMask field including the modifier keys being pressed. The SWT.CTRL constant for Ctrl.
So:
if ((e.stateMask & SWT.CTRL) == SWT.CTRL)
Tests for the Ctrl key being pressed

How to intercept system keypress in a GTK application

I am working on a Vala GTK application that starts minimized by default and I want to bind a specific keyword shortcut to bring the minimized window to the front.
I am able to handle Keyboard events using Accelerators when the app is focus ed, but I am unable to intercept any key press from the system when the app is minimized.
How can I make the app listen to system keyboard events so I can detect the key press accordingly?
Thank you.
I took a look at the source for Ideogram to see how it registers Super-e as a hot-key. It looks to be basically the following, including first checking that a custom hot-key has not already been registered for the application.
// These constants are set at class level.
public const string SHORTCUT = "<Super>e";
public const string ID = "com.github.cassidyjames.ideogram";
// Set shortcut within activate method.
CustomShortcutSettings.init ();
bool has_shortcut = false;
foreach (var shortcut in CustomShortcutSettings.list_custom_shortcuts ()) {
if (shortcut.command == ID) {
has_shortcut = true;
return;
}
}
if (!has_shortcut) {
var shortcut = CustomShortcutSettings.create_shortcut ();
if (shortcut != null) {
CustomShortcutSettings.edit_shortcut (shortcut, SHORTCUT);
CustomShortcutSettings.edit_command (shortcut, ID);
}
}
It uses a CustomShortcutSettings class included in its source to handle the reading and writing to the system settings. The class originated in another application called Clipped.

Eclipse E4 RCP StyledText obtain INSERT KEY state

For an RCP E4 Text Editor application implemented with a StyledText/SourceViewer it is necessary receive the status of the inset key.
Once received the state (insert, smart-insert), the application shall modify the cursor icon and notify other parts the INSERT state (i.e. notify to the status bar control like in a normal plain text editor behavior).
SWT.INSERT only listens for the key to be pressed, but nothing if the StyledText is in INSERT MODE.
styledText.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.keyCode == SWT.INSERT){
System.out.println("INSERT KEY PRESSED!!!");
}
}
};
I have avoided to extend
org.eclipse.ui.texteditor.AbstractTextEditor
and use the method
getInsertMode()
since the application is intended to be pure E4 text editor.
Any hint?
Thanks in advance
First off you need to tell the StyledText not to do the default action when it sees the Insert key:
textWidget.setKeyBinding(SWT.INSERT, SWT.NULL);
Next you need to define a Command, Handler and Key Binding in the context for the editor to deal with the Insert key.
The Handler for the insert command can update the status display and shoyld then tell the StyledText to update the overwrite mode:
textWidget.invokeAction(ST.TOGGLE_OVERWRITE);
Also note that Mac keyboards don't have an Insert key!
Since I found some difficulties to deal with the INSERT_KEY within a sourceviewer control for E4 RCP text editor, I will write extra details to gregg449's answer (great help from him as everytime!).
Following the above answer, I have created Binding Context, Binding Table, Command, Handler and added the Binding Context to the required Part (the part implementing the SourceViewer).
The next code is for SourceViewer and InserKey Handler:
public class CheckKeyBindingSourceViewer extends ITextEditorPart{
public SourceViewer sv = null;
public StyledText st = null;
#PostConstruct
public void postConstruct(Composite parent) {
sv = new SourceViewer(parent, null, null, true, SWT.MULTI | SWT.V_SCROLL |SWT.H_SCROLL);
IDocument doc = new Document("");
sv.setDocument(doc);
st = sv.getTextWidget();
//tell the StyledText not to do the default action when it sees the Insert key
st.setKeyBinding(SWT.INSERT, SWT.NULL);
}
}
public class InsertKeyHandler {
#Execute
public void execute(#Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
if (activePart.getObject() instanceof ITextEditorPart){
ITextEditorPart theSourceViewer = (ITextEditorPart) activePart.getObject();
theSourceViewer.st.invokeAction(ST.TOGGLE_OVERWRITE);
//TODO
//Change cursor sourcewiewer, notify to Statusbar...
}
}
}
The next figure shows the Application.e4xmi with the Binding Context and Binding Table created.
Note that if you do not add the supplementary tag "type:user" to the Binding Table, the bindings are not working at all.
This is not reflected into vogella's tutorial (http://www.vogella.com/tutorials/EclipseRCP/article.html) neither his book.
The only place I found this information was at stackoverflow question:
eclipse rcp keybindings don't work
I'm using eclipse Mars (4.5.0) for both Linux and Windows, I do not know if for newer verions this 'bug' is solved.

GWT: Back Button working twice, both by browser and my GWT code

The Scenario:
In my GWT webapp, I'm using KeyDownHandler to capture the event of user hitting backspace.
Say, I'm using it on widget 'B', and hitting the backpsace when widget 'B' is focused should take me to widget 'A'.
The Problem:
On hitting backspace, I'm taken to widget 'A', BUT only for a moment before the Browser takes me back to the previous page! I want my backspace event to be used only by my (GWT) code, not the browser.
final TextBox txtA = new TextBox();
TextBox txtB = new TextBox();
VerticalPanel testPanel = new VerticalPanel();
testPanel.add(txtA);
testPanel.add(txtB);
txtB.addKeyDownHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE){
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
public void execute() {
txtA.setFocus(true);
}
});
}
}
});
RootPanel.get().add(testPanel);
This SO post might be usefull to you. However its still better to use shift+tab to navigate backwards in a web-based form IMHO.
You might have to prevent the default action from happening. And you might have to do it for some or all of the key events (keydown, keypress –in Firefox–, keyup); be sure to test as many browsers as possible!
That being said, hijacking global keyboard shortcuts is seen my many users as being too intrusive; and it might have consequences on accessibility of your app.

GWT: handling more than one events on Label

I want handle events on a Label when user holds down some key (Ctrl) and then clicks the mouse button together (Ctrl + mouse click), like open some window etc...
How could i do that in GWT? Should i get add two handlers or can do it with one?
thank you.
al
In your click handler you can check if the Ctrl key is pressed when the event was fired, see example below. You also might want to check for the specific mouse button the user clicked on. I've also added that to the example:
yourLabel.addClickHandler(new ClickHandler() {
if(NativeEvent.BUTTON_LEFT == event.getNativeButton() &&
event.isControlKeyDown()) {
//do what you want
}
});
Or for older version of GWT instead of event.isControlKeyDown use event.getNativeEvent().getCtrlKey(), which returns a boolean value true if the control key is pressed when this event is fired.
Edit: this code is buggy, please look at Hilbrand's answer
To be honest, I don't think you can do it with 1 or 2 handlers. I think you would need 3 handler.
A KeyDownHandler that sets a boolean you can later read form the MouseDownHandler
A MouseDownHandler that does what you want
A KeyUpHandler that resets the value of the boolean in the KeyDownHandler
boolean ctrlPressed;
yourLabel.addDomHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
if(event.getAssociatedType().equals(KeyCodes.KEY_CTRL))
ctrlPressed=true;
}
}, KeyDownEvent.getType());
yourLabel.addDomHandler(new KeyUpHandler() {
public void onKeyUp(KeyUpEvent event) {
if(event.getAssociatedType().equals(KeyCodes.KEY_CTRL))
ctrlPressed=false;
}
}, KeyUpEvent.getType());
yourLabel.addClickHandler(new ClickHandler() {
if(ctrlPressed) {
//do what you want
}
});