Results 1 to 18 of 18

Thread: API call to paste?

  1. #1

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133

    Post

    ok, i want to know if there is a way to paste without using senkeys "^{V}" or sendkeys "+{INSERT}"

    currently i can hit a hotkey and my program sets text to the clipboard and sends "^{V}" and i can paste to places when i'm NOT in my program.

    this doesn't work for internet explorer, though... it just sits there and does nothing. however if i'm in notepad it will work just fine.

    so, IS there an api call to paste so i can paste text -from- my program to a different program?

  2. #2
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Well... I didn't look for the API commands (I'm sure there are some), but you can use VB's Clipboard object...

    Code:
    Clipboard.Clear
    Clipboard.SetData "It works!"
    
    Text1 = ClipBoard.GetData
    Well, I'm not that sure if it's the exact use but the calls should be right... (I just tried to start VB6 but there's a MsgBox 'Object doesn't support this method' or something if I want to start and then I can't use any object... )

    Hope this helps

  3. #3

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    i posted here thinking that i could get an api answer to my api question... silly me.

    i don't need to paste into my program i need to paste into other programs using tha api.

    just give me the api, the api is what i need

  4. #4
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    API paste, use SendMessage:
    Code:
    Private Const WM_PASTE As Long= &H3Ø2
    
    Private Declare Function SendMessage _
                    Lib "user32" _
                    Alias "SendMessageA" _
                    (ByVal hwnd As Long, ByVal wMsg As Long, _
                    ByVal wParam As Long, lParam As Any) As Long
    
    
    'Note that this will only paste into the foreign Window,
    'if you want to paste into a TextBox then you will need to
    'retrieve the handle to that instead and pass it in to this
    'function.
    Private Sub PasteForeignWindow(hForeignWnd As Long)
       Call SendMessage(hForeignWnd, WM_PASTE, 0, ByVal 0)
    End Sub
    Hope this is what you wanted, if you need to elaborate just ask!
    Later.

    [Edited by SonGouki on 04-27-2000 at 03:50 PM]

  5. #5

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    hmm.. i wasn't able to get that to work.. perhaps you could play with a small version of my program to get it to work correctly?

    in Form1
    --------------------
    Private Sub Form_Load()
    lPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassedWnd)
    Call RegisterHotKey(hwnd, 1, MOD_CONTROL Or MOD_SHIFT, vbKey1)
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    Call UnregisterHotKey(hwnd, 1)
    Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndProc)
    End Sub
    --------------------

    in a module
    --------------------
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

    Private Const WM_HOTKEY = &H312
    Public Const GWL_WNDPROC = (-4)
    Public Const MOD_ALT = &H1
    Public Const MOD_CONTROL = &H2
    Public Const MOD_SHIFT = &H4
    Public Const MOD_WIN = &H8
    Public lPrevWndProc As Long


    Function SubClassedWnd(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    If Msg = WM_HOTKEY And wParam > 0 Then
    Clipboard.Clear
    Clipboard.SetText (Form1.Text1)

    'i need something to paste here [SendKeys "^{V}"

    End If
    SubClassedWnd = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
    End Function
    -----------------------

    in the form there is 1 text box called Text1

    if you could help me out it'd be great if not

  6. #6

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    (in code form)

    in Form1
    --------------------
    Code:
    Private Sub Form_Load()
         lPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassedWnd)
         Call RegisterHotKey(hwnd, 1, MOD_CONTROL Or MOD_SHIFT, vbKey1)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
         Call UnregisterHotKey(hwnd, 1)
         Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndProc)
    End Sub
    --------------------

    in a module
    --------------------
    Code:
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    Private Const WM_HOTKEY = &H312
    Public Const GWL_WNDPROC = (-4)
    Public Const MOD_ALT = &H1
    Public Const MOD_CONTROL = &H2
    Public Const MOD_SHIFT = &H4
    Public Const MOD_WIN = &H8
    Public lPrevWndProc As Long
    
    
    Function SubClassedWnd(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        
        If Msg = WM_HOTKEY And wParam > 0 Then
                Clipboard.Clear
                Clipboard.SetText (Form1.Text1)
    
        'i need something to paste here [SendKeys "^{V}"       
    
        End If
        SubClassedWnd = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
    End Function
    -----------------------

    in the form there is 1 text box called Text1

  7. #7
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    OK, now I can see the full picture of what you were trying to do, and all I can tell you is that it is difficult.
    The SendMessage API with the message WM_PASTE was the correct tip, however, in order to paste into something you
    need the handle of a control that can accept the event (eg. a TextBox). Now, in your own project this is generally not
    a problem, however in a seperate app it is. The problem is that you need to, somehow, get the handle to the Control
    that you want to paste. My idea on this was to get the handle of the Control that currently has the focus when you
    press the HotKeys, however, I could not find an API call that would return it. I tried a number of them to no avail,
    sorry Perhaps somebody else can give you a hand on this specific topic if you post another thread (try both this
    forum and the General one). Let me know if you find a way.

    Later.

  8. #8

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133

    Angry

    i'm cursed

  9. #9
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Unhappy

    Don't worry, I haven't given up on this quite yet. This time I tried to take a different approach, more along the
    SendKeys method. I reasoned that through API you could simulate the <SHIFT><INSERT> paste method into the control
    that currently had focus. However, just like SendKeys, for some strange reason it doesn't work when implemented in the
    callback method. I think that finding the reason behind this will solve your problem. Until then I'll keep playing
    with it, there must be a solution
    Dan PM
    Analyst Programmer

    VB6 SP3 (also VB4 16-bit sometimes )

  10. #10

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    cursed, i tell you, cursed!
    maybe i'll go buy a book of magic...

  11. #11

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133

    well...

    any new developements?
    anyone know how i can do this?
    even if it aint api i'll be happy if it works

  12. #12

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    how about the keybd_event, could that be worked in with hotkeys?
    if someone could try this for me it'd be appreciated... 2:30 came early =P

    here's a good page for checking the kbd_event out...

    http://www.vbapi.com/ref/k/keybd_event.html

  13. #13
    Lively Member
    Join Date
    Apr 2000
    Location
    Hell
    Posts
    89
    SendKeys works by using keybd_event, so if SK doesn't work, neither will keybd_event. Now, to get the control that has the focus:


    [list=1][*] Use GetForegroundWindow() to get the window with focus. This returns the handle of an actual window (one without the WS_CHILD style).[*] Use GetWindowThreadProcessId() to get that window's thread.[*] Use GetWindowThreadProcessId() to get your own window's thread.[*] Use AttachThreadInput() to bind the two together so you can do some dirty sneaky stuff.[*] Use GetFocus() to get the focus.[*] Use AttachThreadInput() to un-bind the two threads.[/list=1]

    Code:
    Private Function GetFocusWindow() As Long
      ' Returns -1 on failure
      ' Returns 0 if there is no foreground window (pretty rare)
      ' Returns the handle otherwise
      ' Should work in Win95+
      
      Dim hWndFocus As Long, hWndFG As Long
      Dim thrdIDFG As Long, thrdID As Long
      
      hWndFG = GetForegroundWindow()
      thrdID = GetWindowThreadProcessId(Me.hWnd, ByVal 0&)
      thrdIDFG = GetWindowThreadProcessId(hWndFG, ByVal 0&)
      
      ' Attach the thread inputs if necessary
      If (thrdID <> thrdIDFG) Then ' Can't attach to ourselves!
         If AttachThreadInput(thrdID, thrdIDFG, 1) = 0 Then GetFocusWindow = -1: Exit Function
      End If
      
      hWndFocus = GetFocus()
      
      ' Detach
      If (thrdID <> thrdIDFG) Then
        AttachThreadInput thrdID, thrdIDFG, 0
      End If
      
      GetFocusWindow = hWndFocus
    End Function
    - Steve

    Real programmers use COPY CON PROGRAM.EXE

  14. #14

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    ok, i'm not sure what you were teklling me to do... so i just incorperated my program with what Gouki said about the wm_paste and added your stuff to find the foreign window.. didn't work... it still wont paste into a window other than a visualbasic window...
    i'l mention that me.hwnd didn't work, i changed it to form1.hwnd... butr like i said, not quite sure what you wanted me to do...

    here's how the program came together:
    http://www.geocities.com/r337m0nk3y/paste.zip

    in the form (add a text box, text1):
    Code:
    Private Sub Form_Load()
        lPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassedWnd)
        Call RegisterHotKey(hwnd, 1, MOD_CONTROL Or MOD_SHIFT, vbKey1)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Call UnregisterHotKey(hwnd, 1)
        Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndProc)
    End Sub
    in the module:
    Code:
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    Public Declare Function GetForegroundWindow Lib "user32" () As Long
    Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Public Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
    Public Declare Function GetFocus Lib "user32" () As Long
    
    
    Private Const WM_HOTKEY = &H312
    Public Const GWL_WNDPROC = (-4)
    Public Const MOD_ALT = &H1
    Public Const MOD_CONTROL = &H2
    Public Const MOD_SHIFT = &H4
    Public Const MOD_WIN = &H8
    Public lPrevWndProc As Long
    Private Const WM_PASTE As Long = &H302
    
    Private Declare Function SendMessage _
                    Lib "user32" _
                    Alias "SendMessageA" _
                    (ByVal hwnd As Long, ByVal wMsg As Long, _
                    ByVal wParam As Long, lParam As Any) As Long
    
    
    'Note that this will only paste into the foreign Window,
    'if you want to paste into a TextBox then you will need to
    'retrieve the handle to that instead and pass it in to this
    'function.
    Private Sub PasteForeignWindow(hForeignWnd As Long)
       Call SendMessage(hForeignWnd, WM_PASTE, 0, ByVal 0)
    End Sub
    
    
    Function SubClassedWnd(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Dim h*** As Long
    
    If Msg = WM_HOTKEY And wParam > 0 Then
    Clipboard.Clear
    Clipboard.SetText (Form1.Text1)
    
    'i need something to paste here [SendKeys "^{V}"
    
    h*** = GetFocusWindow()
    Call PasteForeignWindow(h***)
    
    End If
    SubClassedWnd = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
    End Function
    
    
    Private Function GetFocusWindow() As Long
      ' Returns -1 on failure
      ' Returns 0 if there is no foreground window (pretty rare)
      ' Returns the handle otherwise
      ' Should work in Win95+
      
      Dim hWndFocus As Long, hWndFG As Long
      Dim thrdIDFG As Long, thrdID As Long
      
      hWndFG = GetForegroundWindow()
      thrdID = GetWindowThreadProcessId(Form1.hwnd, ByVal 0&)
      thrdIDFG = GetWindowThreadProcessId(hWndFG, ByVal 0&)
      
      ' Attach the thread inputs if necessary
      If (thrdID <> thrdIDFG) Then ' Can't attach to ourselves!
         If AttachThreadInput(thrdID, thrdIDFG, 1) = 0 Then GetFocusWindow = -1: Exit Function
      End If
      
      hWndFocus = GetFocus()
      
      ' Detach
      If (thrdID <> thrdIDFG) Then
        AttachThreadInput thrdID, thrdIDFG, 0
      End If
      
      GetFocusWindow = hWndFocus
    End Function
    think maybe you could get it to work?
    if not just thump me upside the head and tell me what to do
    thanks

  15. #15
    Lively Member
    Join Date
    Apr 2000
    Location
    Hell
    Posts
    89
    Hmm...exactly what are you trying to paste into anwyay?
    - Steve

    Real programmers use COPY CON PROGRAM.EXE

  16. #16

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    Originally posted by cwm
    this doesn't work for internet explorer, though...
    an internet explorer text box... it doesn't seem to work with icq, either...
    in short, not a text box in my program.

  17. #17
    Lively Member
    Join Date
    Apr 2000
    Location
    Hell
    Posts
    89
    Ohhhhhhhhhhhhhhhhhhhhhhhhhhh.

    Internet Explorer text boxes are 'windowless controls'. You can't do **** to them. They don't even have window handles.
    - Steve

    Real programmers use COPY CON PROGRAM.EXE

  18. #18

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    well, i'd believe that and give up had i not seen a program do exactly what i'm trying to do...
    oh well, another one falls before the mighty hand of simple pasting =P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width