forked from vrapper/vrapper
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command/handler which sends out arrow key presses when triggered
We have had requests from people for easier ways to navigate the package explorer, preferably using the HJKL keys like Vim does. This commit adds a "Vrapper Up Arrow Map" command (and so on for the other 3 arrows) to the General > Keys preference page which can be bound to HJKL. Do note that it should be bound to the "Vrapper Enabled on a View" context to be safe; otherwise you would be unable to type HJKL in any dialog or entry box. As a safety measure the code will also fall back to just inserting the bound keys if Vrapper is disabled or when the focused control is a textbox. Other controls have not been tested because they're not commonly available in Eclipse Views. Fixes vrapper#173.
- Loading branch information
Showing
3 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...e.vrapper.eclipse/src/net/sourceforge/vrapper/eclipse/actions/VrapperArrowMapHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package net.sourceforge.vrapper.eclipse.actions; | ||
|
||
import org.eclipse.core.commands.AbstractHandler; | ||
import org.eclipse.core.commands.ExecutionEvent; | ||
import org.eclipse.core.commands.ExecutionException; | ||
import org.eclipse.swt.SWT; | ||
import org.eclipse.swt.custom.StyledText; | ||
import org.eclipse.swt.widgets.Event; | ||
import org.eclipse.swt.widgets.Text; | ||
import org.eclipse.swt.widgets.Widget; | ||
|
||
import net.sourceforge.vrapper.log.VrapperLog; | ||
|
||
/** | ||
* Remaps an kind of key into the press of an Up / Down / Left / Right arrow key. | ||
*/ | ||
public class VrapperArrowMapHandler extends AbstractHandler { | ||
|
||
@Override | ||
public Object execute(ExecutionEvent event) throws ExecutionException { | ||
String commandId = event.getCommand().getId(); | ||
|
||
if ( ! (event.getTrigger() instanceof Event)) { | ||
return null; | ||
} | ||
Event triggerEvent = (Event) event.getTrigger(); | ||
if (triggerEvent.type != SWT.KeyDown) { | ||
VrapperLog.debug("Shortcut handler received an activation other than key type?!?"); | ||
return null; | ||
} | ||
|
||
Widget widget = triggerEvent.widget; | ||
|
||
// Act as if we're in Insert mode if textbox is focused: insert input key in widget | ||
if (widget instanceof Text || widget instanceof StyledText) { | ||
// Check that this command is bound to a Unicode character (and not F1 or something) | ||
if ((triggerEvent.stateMask & SWT.MODIFIER_MASK) == 0 | ||
&& (triggerEvent.keyCode & SWT.KEYCODE_BIT) == 0) { | ||
|
||
String input = new StringBuilder().appendCodePoint(triggerEvent.keyCode).toString(); | ||
if (widget instanceof Text) { | ||
Text textBox = (Text) widget; | ||
textBox.insert(input); | ||
} else if (widget instanceof StyledText) { | ||
StyledText styledText = (StyledText) widget; | ||
styledText.insert(input); | ||
} | ||
} | ||
} else { | ||
|
||
Event mappedEvent = null; | ||
// This handler can accept 4 commands (up/down/left/right) and translates them into arrow key presses | ||
if (commandId.endsWith(".up")) { | ||
mappedEvent = keyEvent(SWT.ARROW_UP); | ||
} else if (commandId.endsWith(".down")) { | ||
mappedEvent = keyEvent(SWT.ARROW_DOWN); | ||
} else if (commandId.endsWith(".right")) { | ||
mappedEvent = keyEvent(SWT.ARROW_RIGHT); | ||
} else if (commandId.endsWith(".left")) { | ||
mappedEvent = keyEvent(SWT.ARROW_LEFT); | ||
} | ||
if (mappedEvent != null) { | ||
triggerEvent.display.post(mappedEvent); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static Event keyEvent(int keyCode) { | ||
Event result = new Event(); | ||
result.type = SWT.KeyDown; | ||
result.keyCode = keyCode; | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters