Reputation: 59
My wxpython application must respond everytime user hits CTRL-C, no matter if applications frame is on top/visible but unfocussed/minimalized/under other window etc. Basically I want to know that user copied something into clipboard using CTRL-C combination - other changes in clipboard (like mouse r-click + "copy" should be ignored), than do things with data copied into clipboard. That's why I'm using pyHook and everything seems to be fine except... all code within "OnKeyboardEvent" seems to be executed before CTRL-C does its "actuall job" (copying things into clipboard), so every time I am kind of "one step back":
What happens:
1. user hits CTRL-C
2. my "OnKeyboardEvent" code is executed
3. data is being copied to the clipboard (CTRL-C does its job)
I need to do 3. before 2. .... :)
Anyway, here's the code:
import wx
import pyHook
import win32clipboard
class TextFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Frame', size=(300, 100))
panel = wx.Panel(self, -1)
self.basicText = wx.TextCtrl(panel, -1, "", size=(175, -1))
self.basicText.SetValue("Press CTRL-C")
hm = pyHook.HookManager()
hm.KeyDown = self.OnKeyboardEvent
hm.HookKeyboard()
def OnKeyboardEvent(self,event):
if event.Ascii == 3:
win32clipboard.OpenClipboard()
clipboarditem = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print clipboarditem
self.basicText.SetValue(clipboarditem)
app = wx.PySimpleApp()
frame = TextFrame()
frame.Show()
app.MainLoop()
Second thing wrong with code above... See this "print clipboarditem" at the end of "OnKeyboardEvent" procedure? If I delete it next command - "self.basicText.SetValue(clipboarditem)" stops working and gives
line 23, in OnKeyboardEvent
self.basicText.SetValue(clipboarditem)
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 13075, in SetValue
return _core_.TextEntryBase_SetValue(*args, **kwargs)
TypeError: an integer is required
which is mind blowing for me :/
Upvotes: 1
Views: 1248
Reputation: 140276
Took me a while to figure it out but did it!
Just change key down event to key up and it works. Your callback is called when CTRL+C is released, so clipboard is already correct since CTRL+C has been processed.
(also fixed the callback returned True
else I get a lot of exception messages)
import wx
import pyHook
import win32clipboard
import time
class TextFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Frame', size=(300, 100))
panel = wx.Panel(self, -1)
self.basicText = wx.TextCtrl(panel, -1, "", size=(175, -1))
self.basicText.SetValue("Press CTRL-C")
hm = pyHook.HookManager()
hm.KeyUp = self.OnKeyboardEvent # key up!!
hm.HookKeyboard()
def OnKeyboardEvent(self,event):
if event.Ascii == 3:
print("control c released")
win32clipboard.OpenClipboard()
clipboarditem = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print("contents "+clipboarditem)
self.basicText.SetValue(clipboarditem)
return True
app = wx.PySimpleApp()
frame = TextFrame()
frame.Show()
app.MainLoop()
Upvotes: 1