datagridview - Pressing Ctrl + C on computer.keyboard -
so building tool called "clipboard history"
my target is, when copy anywhere(web, pc text etc) want add my.computer.clipboard datagridview.
everything fine need command or recognizing ctrl + c (in pckeyboard, not form active) , once (form1.datagridview1.rows.add(dateandtime.now, clipboard.gettext)
private sub timer2_tick(sender object, e eventargs) handles timer2.tick dim keydata keys if my.computer.keyboard.ctrlkeydown andalso keydata.c form1.datagridview1.rows.add(dateandtime.now, clipboard.gettext) end if end sub
thank :)
there no standard .net library know of can accomplish want. using windows api function addclipboardformatlistener can receive notification of items placed on clipboard.
addclipboardformatlistener requires window handle send notification. use class derived system.windows.forms.nativewindow listen wm_clipboardupdate. class defined below, expose event
named clipboarddataavailable can subscribed , passed argument dataobject can used retrieve clipboard contents.
imports system.runtime.interopservices public class clipboardlistener : inherits nativewindow implements idisposable public event clipboarddataavailable(data dataobject) public sub new() me.createhandle(new createparams()) addclipboardformatlistener(me.handle) end sub public overrides sub destroyhandle() me.dispose() mybase.destroyhandle() end sub protected overrides sub wndproc(byref m message) const wm_clipboardupdate int32 = &h31d if m.msg = wm_clipboardupdate dim data dataobject = ctype(clipboard.getdataobject, dataobject) raiseevent clipboarddataavailable(data) end if mybase.wndproc(m) end sub <dllimport("user32.dll", setlasterror:=true)> _ public shared function addclipboardformatlistener(hwnd intptr) <marshalas(unmanagedtype.bool)> boolean end function <dllimport("user32.dll", setlasterror:=true)> _ public shared function removeclipboardformatlistener(hwnd intptr) <marshalas(unmanagedtype.bool)> boolean end function #region "idisposable support" private disposedvalue boolean ' detect redundant calls ' idisposable protected overridable sub dispose(disposing boolean) if not me.disposedvalue removeclipboardformatlistener(me.handle) end if me.disposedvalue = true end sub ' todo: override finalize() if dispose(byval disposing boolean) above has code free unmanaged resources. protected overrides sub finalize() ' not change code. put cleanup code in dispose(byval disposing boolean) above. dispose(false) mybase.finalize() end sub ' code added visual basic correctly implement disposable pattern. public sub dispose() implements idisposable.dispose ' not change code. put cleanup code in dispose(disposing boolean) above. dispose(true) gc.suppressfinalize(me) end sub #end region end class
example usage:
public class form1 private withevents listener new clipboardlistener private sub listener_clipboarddataavailable(data dataobject) handles listener.clipboarddataavailable if data.containstext debug.print(data.gettext) end sub end class
Comments
Post a Comment