View Single Post
Old Feb 23rd, 2010, 10:35 PM   #13
LeandroA
Lively Member
 
Join Date: Dec 08
Posts: 87
LeandroA will become famous soon enough (50+)
Re: Save to and replay from function keys? (Macro)

Using the clipboard is as fast and safe, to have some fun, another method using WM_CHAR

put a textbox on a form, run it, we focus on a window, notepad, word, or any html editor and then press f10

Code:
Option Explicit

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Type GUITHREADINFO
    cbSize As Long
    flags As Long
    hwndActive As Long
    hwndFocus As Long
    hwndCapture As Long
    hwndMenuOwner As Long
    hwndMoveSize As Long
    hwndCaret As Long
    rcCaret As RECT
End Type

Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Type Msg
    hwnd As Long
    Message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type

Private Declare Function GetGUIThreadInfo Lib "user32.dll" (ByVal idThread As Long, ByRef pgui As GUITHREADINFO) As Long
Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long

Private Const WM_CHAR       As Long = &H102
Private Const WM_HOTKEY     As Long = &H312
Private Const PM_REMOVE     As Long = &H1

Private bCancel As Boolean

Private Sub ProcessMessages()
    Dim Message As Msg
    Dim HANDLE  As Long
    
    Do While Not bCancel

        WaitMessage
        If PeekMessage(Message, Me.hwnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
        
            HANDLE = GetFocusHandle
            
            If HANDLE <> 0 Then
                SetWindowTextEx HANDLE, Text1.Text  '<<<<<<<this line put the text in other aplication
            End If
            
        End If

        DoEvents
    Loop
End Sub


Public Function GetFocusHandle() As Long
    Dim GTI As GUITHREADINFO
    GTI.cbSize = Len(GTI)
    GetGUIThreadInfo GetWindowThreadProcessId(GetForegroundWindow, 0), GTI
    GetFocusHandle = GTI.hwndFocus
End Function

Private Function SetWindowTextEx(hwnd As Long, Text As String)
Dim i As Integer
Dim Key As Byte

For i = 1 To Len(Text)
    Key = Asc(Mid(Text, i, 1))
    If Key <> 10 Then
        PostMessage hwnd, WM_CHAR, Key, 0&
    End If
Next
End Function

Private Sub Command1_Click()
Timer1.Interval = 5000
End Sub

Private Sub Form_Load()

    bCancel = False

    Call RegisterHotKey(Me.hwnd, &HBFFF&, 0&, vbKeyF10)

    Me.Show

    ProcessMessages
End Sub

Private Sub Form_Unload(Cancel As Integer)
    bCancel = True
    Call UnregisterHotKey(Me.hwnd, &HBFFF&)
End Sub

es solo un ejemplo pero es mas conveniente utilizar sublcass para los hotkeys
LeandroA is offline   Reply With Quote