Home > Software engineering >  HOOK to achieve the clipboard function abnormal.
HOOK to achieve the clipboard function abnormal.

Time:09-18

I want to implement a function: through the HOOK API, intercepted keyboard message "F4", and perform your own code, the code will be automatically click the mouse to copy, a page on a string, (selection, and press CTRL + C), and then will be copied to the clipboard content of memory variables,. I have this piece of code is implemented by EXCEL VBA, calls the WINDOWS API, but it is always unstable, I found that sometimes can be copied, sometimes not to be able to copy, sometimes it when the current window artificial selection press CTRL + C to copy the contents of a string, not the expected target string on the web, what's the problem?? Never want to understand, please analyse, thanks a lot!

Before is the selected string mouse code and send CTRL + C code, followed by the clipboard memory

SetCursorPos X, y

Const MOUSEEVENTF_LEFTDOWN=& amp; The H2
Const MOUSEEVENTF_LEFTUP=& amp; H4

Const MOUSEEVENTF_RIGHTDOWN=& amp; H8
Const MOUSEEVENTF_RIGHTUP=& amp; H10

Mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

Mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

'Sleep 4000

Call keybd_event (17, 0, 0, 0) '//press CTRL
Call Delay (100)
Call keybd_event (67, 0, 0, 0) '//press c
Call Delay (100)
Call keybd_event (67, 0, 2, 0) 'release//c
Call Delay (100)
Call keybd_event (17, 0, 2, 0) 'release//CTRL

OpenClipboard ByVal 0 & amp;
If IsClipboardFormatAvailable (CF_UNICODETEXT) Then
HMem=GetClipboardData (CF_UNICODETEXT)
LpData=https://bbs.csdn.net/topics/GlobalLock (hMem)
NSize=GlobalSize (hMem)
Destnation_string=String (nSize, 0)
CopyMemory ByVal StrPtr (destnation_string), ByVal lpData, ByVal nSize
GlobalUnlock hMem
Eoms_id=left (eoms_id, InSt (destnation_string, CRH (0)) - 1)
End the If

The Debug. Print destnation_string

CloseClipboard

CodePudding user response:

Your continuous several mouse_event, it has a problem!
I think there should be a "interval",

Also, before you this SetCursorPos x, y, transfer coordinates is correct?

CodePudding user response:

Confirmed that has nothing to do with the mouse_event, I just want to let it point 3 under the mouse, the string selection, this is what I expected, no problem, I also found a rule, I ordered the F4, when it is copied to the variable not I selected and press CTRL + C in content, but the point before the F4, previous clipboard contents of the original content, and then point F4, is I expected string, is actually step point on the F4 duplicate content, please before OpenClipboard calls, is to call the first: "EmptyClipboard"?

CodePudding user response:

I don't think call EmptyClipboard,
Your idea is "future generations," Ctrl + C to copy to clipboard, obtain "clipboard text content" again!
Use Ctrl + C, is through the "system", it will deal with these, don't wrong,
I only found VB6 "clipboard object" is this the problem of "not reliable", in using it to modify "the clipboard contents," sometimes "unsuccessful".
But if the "empty" clipboard first, and then "add new content", don't seem to have failed,
And this code, you worth my "suspect", is your three combo "mouse"...
It should be: first click, activate the window; Then double-click "" achieve future generations!
I think you like three combo seems to have a problem: the "time" is too short, too late response by the operating window (likely "leak"),
Does not rule out is "translation" is not appropriate, leading to the possibility of not operating correctly, your three combo a double-click + click,
I would suggest that you after the first mouse press/release, Sleep more than 500 ms (the system default "double-click interval" seems to be 500 ms, can be adjusted according to the system actual value),
And you can add other DoEvents, after Sleep between "double click", after may without delay, or may delay test under 10 to 30 ms,
Behind you "keyboard", need not every time delay 100 ms, instead of the previous two delay 20 ms enough;" Let go of the button "of the two step, without any delay,

CodePudding user response:

Problem is not here, in fact, CTRL + C operations, has the content is copied to the clipboard, next time I press F4 is I copied the content of the previous point F4, I confirm that it has been successfully copied the specified string, but is lagging behind the time, every time is to get to the contents of the clipboard at a time, before this rule I have repeatedly tested, 100% is that I suspect this is associated with hook mechanism,
Keyboard hook callback function:
The Public Function KeyboardCallback (ByVal Code As Long, ByVal wParam As Long, ByVal lParam) As Long As Long
If wParam=WM_KEYUP Then
If Hookstruct. VkCode=VK_F4 Then
My_result=my_copy_string
End the If
End the Function
end if

CodePudding user response:

Reason: hMem=GetClipboardData (CF_UNICODETEXT), before of this instruction is A the contents of the clipboard, and made A statement before the "CTRL + C" event, and the operating system the step to the cache, it had no immediate execution copy the string to the clipboard operations, but is first performed "hMem=GetClipboardData (CF_UNICODETEXT)", this line of code, or CTRL + C after the incident, the selected operating system is the need for A certain amount of time A string from the process of A copy to the clipboard, and the following GetClipboardData, perform very quickly, so get or the old, the analysis of ShangZhang, certainly there will be A lag phenomenon, also please correct my analysis make sense?

CodePudding user response:

I feel you say "lag" is not reasonable!


You can have a try in your SetCursorPos x, y "empty" clipboard, there will be after the end of "operation", no copy to things?
You can try: after your "program", and in the "notepad" Ctrl + V,
See if there are any text and paste out? You selected in your web page content?
Pay particular attention to one point: make sure you "by the operating website," before you program operation, there is no "selected content",
If you say "lag" phenomenon, it is your "program operation" for several times, I doubt it is actually a "web page selection" lag caused by operation,

CodePudding user response:

Your 4th floor code: my_result=my_copy_string
Here my_copy_string is a variable, or a function?
If it is a "variable", you should consider whether its value would be the result of storage before;
If this is the "function" (" results "is equivalent to your ZhuTie code), then you have to seriously consider I said on the 6th floor,
  •  Tags:  
  • VBA
  • Related