问题
I need to add some items to the default context menu of a text control in SWT, but already found out I cannot modify this menu and have to create a new one from scratch.
But how do I emulate the default functions Undo, Cut, Copy, Paste, Delete? Do I really have to re-implement all this Clipboard stuff for myself? And I don't even know how to access the Undo history of the control. Is there some maybe dirty hack to emulate the key codes that achieve the functionality?
回答1:
The StyledText
has built-in support for Cut, Copy & Paste:
StyledText editor = new StyledText(...);
editor.invokeAction(ST.CUT);
editor.invokeAction(ST.COPY);
editor.invokeAction(ST.PASTE);
As for the Undo operation, I'm afraid you'll have to implement it yourself. SWT
doesn't have anything useful here, AFAIK. Here's a good start if you want to do it yourself: SWT Undo Redo.
回答2:
I already found out that I can use the functions cut(), copy() and paste() on a Text control. Sadly, undo() is not available.
I now emulate undo by sending CTRL-Z (CTRL-Y on your US-Keyboard) to the app.
来源:https://stackoverflow.com/questions/7179464/swt-how-to-recreate-a-default-context-menu-for-text-fields