SWT: How to recreate a default context menu for text fields

淺唱寂寞╮ 提交于 2019-12-05 02:30:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!