Paste from clipboard on Right Mouse Click

I have an application that the users want to highlight text from one applicaiton, right click the mouse and select copy, then click on a textbox in my FPMI screen right mouse click and select paste. Any way I can duplicate this functionality? I am able to use the standard shortcut keys, ctrl-C and ctrl-V to cut and paste, but they want to use the mouse for this operation instead.

Add this script to the mouseReleased (or to mousePressed also, if you have Mac clients) of your text field:

[code]def doCut(event):
from java.awt import Toolkit
from java.awt.datatransfer import StringSelection
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
clipboard.setContents(StringSelection(event.source.text),None)
event.source.text=""

def doCopy(event):
from java.awt import Toolkit
from java.awt.datatransfer import StringSelection
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
clipboard.setContents(StringSelection(event.source.text),None)

def doPaste(event):
from java.awt import Toolkit
from java.awt.datatransfer import DataFlavor
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
contents = clipboard.getContents(None).getTransferData(DataFlavor.stringFlavor)
event.source.text=contents

menu = fpmi.gui.createPopupMenu([“Cut”,“Copy”,“Paste”], [doCut, doCopy,doPaste])

menu.show(event)[/code]

Hope this helps,

2 Likes

Works Great!!! Thanks a lot.

I thought I would try to enhance Carl’s code :wink: to cope with selecting part of the text in the Text Field, rather than affecting all of the contents at once. Here’s the result:

[code]def doCut(event):
from java.awt import Toolkit
from java.awt.datatransfer import StringSelection
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
clipboard.setContents(StringSelection(event.source.getSelectedText()),None)
text=event.source.text
sel=event.source.getSelectedText()
pos=event.source.getCaretPosition()
length=len(sel)
test=text[pos:(pos+length)]
if test==sel:
#Caret is at the start of the selected text.
string1=text[:pos]
string2=text[(pos+length):]
else:
#Caret is at the end of the selected text.
string1=text[:(pos-length)]
string2=text[pos:]
event.source.text="%s%s" % (string1,string2)

def doCopy(event):
from java.awt import Toolkit
from java.awt.datatransfer import StringSelection
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
clipboard.setContents(StringSelection(event.source.getSelectedText()),None)

def doPaste(event):
from java.awt import Toolkit
from java.awt.datatransfer import DataFlavor
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
contents = clipboard.getContents(None).getTransferData(DataFlavor.stringFlavor)
text=event.source.text
sel=event.source.getSelectedText()
pos=event.source.getCaretPosition()
if sel is None:
length=0
else:
length=len(sel)
test=text[pos:(pos+length)]
if test==sel:
#Caret is at the start of the selected text.
string1=text[:pos]
string2=text[(pos+length):]
else:
#Caret is at the end of the selected text.
string1=text[:(pos-length)]
string2=text[pos:]
event.source.text="%s%s%s" % (string1,contents,string2)

sel=event.source.getSelectedText()
if sel is None:
menu = fpmi.gui.createPopupMenu([“Paste”], [doPaste])
else:
menu = fpmi.gui.createPopupMenu([“Cut”,“Copy”,“Paste”], [doCut, doCopy, doPaste])

menu.show(event)
[/code]
The only problem I can see is that when the operation is finished, the default behaviour is for the whole of the Text Field contents to be highlighted. Is there a way to stop this? I also found that I can’t access the getSelectedText() function for the Text Area component. Is this possible?

Of course, Carl is now going to point out the Java functions that can do all this in just 2 lines :smiley:

Al

Al - excellent work, when I wrote that routine I was really busy so I skipped the details of what text was selected, yours works quite nicely…

but you know I can’t resist a challenge!

[code]def doCut(event):
from java.awt.event import ActionEvent
action = event.source.actionMap.get(“cut”)
action.actionPerformed(ActionEvent(event.source, ActionEvent.ACTION_PERFORMED, action.getValue(action.ACTION_COMMAND_KEY)))

def doCopy(event):
from java.awt.event import ActionEvent
action = event.source.actionMap.get(“copy”)
action.actionPerformed(ActionEvent(event.source, ActionEvent.ACTION_PERFORMED, action.getValue(action.ACTION_COMMAND_KEY)))

def doPaste(event):
from java.awt.event import ActionEvent
action = event.source.actionMap.get(“paste”)
action.actionPerformed(ActionEvent(event.source, ActionEvent.ACTION_PERFORMED, action.getValue(action.ACTION_COMMAND_KEY)))

menu = fpmi.gui.createPopupMenu([“Cut”,“Copy”,“Paste”], [doCut, doCopy,doPaste])

menu.show(event)[/code]

It still has that same select-all issue though, maybe I’ll look into that on Monday…

Well, there’s a graphic example of a real programmer v. an engineer using a computer as a tool :slight_smile:

Al

I am trying to use this code to right click and copy selected rows on a table. how would I go about doing that since a table does not have a text property?

How do you want this “row-copy” to work? Concatenate the columns of the row(s) together into a long string? A copied dataset?

What I have done is made an export button to export to an excel file. What I wanted was to be able to selected the rows in a table, right click copy, and then go to an existing excel file and paste.

Well, I really thought I had something for you. I swear this used to work (Pasting CSV into Excel), but apparently it doesn’t work anymore in Excel 2007. Posting it anyways in case it gets you any farther.

Put something like this is the Table’s mouseReleased event:

[code]def doCopy(event):
if event.source.selectedRow==-1:
return
rows = event.source.selectedRows
ds = event.source.data
toCopy = []
for row in rows:
rowArray = []
for col in range(ds.columnCount):
stringVal = ‘"%s"’ % ds.getValueAt(row,col)
rowArray.append(stringVal)
toCopy.append(",".join(rowArray))
csv = “\n”.join(toCopy)

from java.awt import Toolkit
from java.awt.datatransfer import StringSelection
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
clipboard.setContents(StringSelection(csv),None)

menu = system.gui.createPopupMenu([‘Copy’],[doCopy])
menu.show(event)[/code]