We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Global detect paste (control + V) into another program from my VB app

SamWitney
SamWitney asked
on
Medium Priority
775 Views
Last Modified: 2008-01-09
Hi,

I need to detect when the user PASTES some data.

This is because I have an app that copies some data to the clipboard for the user to paste, but the user than has to go back and copy the next item. I would like to make it so the app detects the paste event, then copys the next item ready for the next paste.

Please help :)

Regards,
Sam
Comment
Watch Question

Leo EikelmanDirector, IT and Business Development

Commented:
well I know how to do this in vb .net with the following

public class MySuperTextBox : TextBox
{
    private const int WM_PASTE = 0x0302;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PASTE)
        {
            // add your own paste handling code here
        }
        else
            base.WndProc(m);
    }
}


maybe u can try to use the above code to understand how to do this in your VBA

Leo
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
But this would break the normal operation of the clipboard.  What if the user wants to use the clipboard for something else while you are "monitoring the clipboard"?

How about setting up a global hotkey combination that would either paste the data for you or put the next item onto the clipboard?

Author

Commented:
Yea... it just needs to monitor the clipboard - on paste - copy the next item.

It does need to do that - no hot key needed... its just copying the username and password, or IP / Username / Password - its for quick access to servers and stuff.

I need a way to globally monitor when the clipboard is pasted (or Control + V) so I get an event to "copy the next item" ready so the user can go, Paste, Paste, Paste and done. They will close the app or press the End Copy button or something so it won't mess up the rest of windows.

By the way I am using VB6.

Many Thanks.
Leo EikelmanDirector, IT and Business Development

Commented:
apparently you can also detect when the user PASTES by using this

If GetAsyncKeyState(17) < 0 And GetAsyncKeyState(86) < 0 Then
      MsgBox "CTRL + V Detected"
    End If

but I have no tested this

Good luck,

Leo

Author

Commented:
If GetAsyncKeyState(17) < 0 And GetAsyncKeyState(86) < 0 Then
      MsgBox "CTRL + V Detected"
End If

Unspecified function error...
Leo EikelmanDirector, IT and Business Development

Commented:
yeah I didn't see your previous post saying you are using vb6, I thought this was a vba.  

I'll do some more digging

Author

Commented:
ah sorry, cheers - much appreciated :)
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
There isn't any way in VB6 to GLOBALLY detect a paste operation.  You could certainly use a Timer to poll for the Ctrl-V combination, but that would not trap pastes done with a context menu.

As leikelman has shown, it is possible to trap the WM_PASTE message, but this can only be targeted at controls within your OWN application.

It is possible to detect when something new has been COPIED to the clipoard by using the SetClipboardViewer() API and looking for the WM_DRAWCLIPBOARD msg.  But this will not tell you when something has been PASTED from the clipboard.

Thus my suggestion for a Hotkey approach.  You press the combination and then the correct data is sent to the currently focus control via SendKeys.  After each hotkey press, you increment a counter so you know to send the next sequence, etc...

I could always be wrong though...wouldn't be the first time.  =)

Author

Commented:
How would I detect the "hot key" and could that hot key be set to "Control + V" or even just V? Or how do I poll for the control + V combination?

That sounds like it would work.. :)

It dosent need to do it from the context menu... just detect either Control+V or V, or a hot key would do too... but remember my app won't have focus.

Regards,
Sam
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Something like...

Option Explicit

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Private Sub Form_Load()
    Timer1.Interval = 50
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    If IsKeyDown(vbKeyControl) And IsKeyDown(vbKeyV) Then
        MsgBox "Ctrl-V pressed"
    End If
End Sub

Private Function IsKeyDown(ByVal key As Long) As Boolean
    Const KeyDownMask As Long = 32768
    Dim state As Long
    state = GetKeyState(key)
    IsKeyDown = ((state And KeyDownMask) > 0)
End Function

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
If you don't want to poll you could install a low level keyboard hook and detect Ctrl-V that way.  This approach involves subclassing and is a little more complex.

Author

Commented:
legend - that works perfect!!

Thank you so much... I have been told by a lot of people VB can't do it... many thanks.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
It does what you asked for but it's not a foolproof approach.  You don't know ~what~ was pasted...just that Ctrl-V was pressed.  What if the clipboard was changed?

Anyhoo...glad I could help.

Author

Commented:
Not a problem - its only so that the app can copy the next item (the password) after the user pastes the username that was copied just before.

Saves going to and from the program to copy username, paste, and back to copy password - and in some cases, URL/domain/path etc too.

Cheers.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.