PDA

Click to See Complete Forum and Search --> : API call to paste?


cwm
Apr 25th, 2000, 06:11 AM
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?

Fox
Apr 25th, 2000, 12:12 PM
Well... I didn't look for the API commands (I'm sure there are some), but you can use VB's Clipboard object...


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

cwm
Apr 26th, 2000, 04:34 AM
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

SonGouki
Apr 27th, 2000, 02:38 AM
API paste, use SendMessage:

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]

cwm
Apr 27th, 2000, 05:11 AM
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 :D if not :(

cwm
Apr 27th, 2000, 05:29 AM
(in code form)

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

SonGouki
Apr 27th, 2000, 09:36 AM
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.

cwm
Apr 27th, 2000, 12:09 PM
i'm cursed :(

SonGouki
Apr 28th, 2000, 01:32 AM
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 :)

cwm
Apr 28th, 2000, 02:38 AM
cursed, i tell you, cursed!
maybe i'll go buy a book of magic...

cwm
May 10th, 2000, 03:04 PM
any new developements?
anyone know how i can do this?
even if it aint api i'll be happy if it works

cwm
May 10th, 2000, 04:37 PM
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

Stevie-O
May 10th, 2000, 07:39 PM
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]


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

cwm
May 11th, 2000, 03:10 AM
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):

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:

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 :D
thanks

Stevie-O
May 11th, 2000, 03:19 AM
Hmm...exactly what are you trying to paste into anwyay?

cwm
May 11th, 2000, 03:51 AM
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.

Stevie-O
May 11th, 2000, 03:54 AM
Ohhhhhhhhhhhhhhhhhhhhhhhhhhh.

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

cwm
May 11th, 2000, 04:04 AM
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