Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

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

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
0
SamWitney
Asked:
SamWitney
  • 6
  • 5
  • 3
1 Solution
 
Leo EikelmanDirector, IT and Business DevelopmentCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
 
SamWitneyAuthor 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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Leo EikelmanDirector, IT and Business DevelopmentCommented:
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
0
 
SamWitneyAuthor Commented:
If GetAsyncKeyState(17) < 0 And GetAsyncKeyState(86) < 0 Then
      MsgBox "CTRL + V Detected"
End If

Unspecified function error...
0
 
Leo EikelmanDirector, IT and Business DevelopmentCommented:
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
0
 
SamWitneyAuthor Commented:
ah sorry, cheers - much appreciated :)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.  =)
0
 
SamWitneyAuthor 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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
SamWitneyAuthor 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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
SamWitneyAuthor 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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now