Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running macro on background
#1
Hello,

I'm trying to make a macro that works on an application (game) and can run on the background. I've found some threads with the "SendKeyToWindow" function, however I can't get it implemented. The problem is that it works perfectly if I'm in a typing environment (like the chat in the game), but it doesn't do anything outside that environment (for example moving the target).
Does anyone has any ideas?
Below is the macro and the SendKeysToWindow function:

Macro
Code:
Select All      Help
int w=act(win("APPLICATION_NAME" "D3D Window"))
rep
    SendKeysToWindow2 w key(6)
    SendKeysToWindow2 w key(V)
    SendKeysToWindow2 w key(V)
    SendKeysToWindow2 w key(V)
    wait 5

key(6) refers to an action and key(V) refers to pressing space.

Function
Code:
Select All      Help
/
function hwnd ARRAY(KEYEVENT)'a

Sends keys to a child window using WM_KEYDOWN/WM_KEYUP messages.
The window can be inactive, but with some windows then does not work.

hwnd - child window handle. Or main window handle, if it receives keys.
a - the <help>key</help> function.

EXAMPLE
int hwnd=id(15 "Notepad")
SendKeysToWindow2 hwnd key("text[]" Cs)


int i lparam m up alt ati
str sk
ifk(A) alt=1

for i 0 a.len
    KEYEVENT k=a[i]
    if(k.flags&0x80) i+1; continue ;;wait
    sel(k.vk)
        case [16,17,18]
        int th1=GetCurrentThreadId
        int th2=GetWindowThreadProcessId(hwnd &i)
        ati=AttachThreadInput(th1 th2 1)
        sk.all(256)
        0.01
        break

for i 0 a.len
    k=a[i]
    
    if(k.flags&0x80) ;;wait
        i+1
        opt waitmsg -1
        wait (a[i].wt/1000.0)
        continue
    
    lparam=k.sc<<16|1
    if(k.flags&KEYEVENTF_EXTENDEDKEY) lparam|0x01000000
    if(k.flags&KEYEVENTF_KEYUP) lparam|0xC0000000; up=1; else up=0
    
    if(alt) m=iif(up WM_SYSKEYUP WM_SYSKEYDOWN); lparam|0x20000000
    else m=iif(up WM_KEYUP WM_KEYDOWN)
    
    if(k.vk=VK_RETURN) 0.01 ;;some controls, eg PSPad, would insert new line too early
    
    PostMessage hwnd m k.vk lparam
    0.01
    
    sel(k.vk)
        case [16,17,18] ;;modifier keys
         wait while processes prev keys
        if(i) SendMessage hwnd 0 0 0
        0.01
        
        GetKeyboardState sk
        sk[k.vk]=iif(up 0 0x80)
        SetKeyboardState sk
    
    if(k.vk=VK_MENU) alt=!up

if(ati) ati=AttachThreadInput(th1 th2 0)
#2
(12-19-2017, 06:35 PM)tatsuoshu Wrote: Hello,

I'm trying to make a macro that works on an application (game) and can run on the background. I've found some threads with the "SendKeyToWindow" function, however I can't get it implemented. The problem is that it works perfectly if I'm in a typing environment (like the chat in the game), but it doesn't do anything outside that environment (for example moving the target).
Does anyone has any ideas?
Below is the macro and the SendKeysToWindow function:

Macro
Code:
Select All      Help
int w=act(win("APPLICATION_NAME" "D3D Window"))
rep
    SendKeysToWindow2 w key(6)
    SendKeysToWindow2 w key(V)
    SendKeysToWindow2 w key(V)
    SendKeysToWindow2 w key(V)
    wait 5

key(6) refers to an action and key(V) refers to pressing space.

Function
Code:
Select All      Help
/
function hwnd ARRAY(KEYEVENT)'a

Sends keys to a child window using WM_KEYDOWN/WM_KEYUP messages.
The window can be inactive, but with some windows then does not work.

hwnd - child window handle. Or main window handle, if it receives keys.
a - the <help>key</help> function.

EXAMPLE
int hwnd=id(15 "Notepad")
SendKeysToWindow2 hwnd key("text[]" Cs)


int i lparam m up alt ati
str sk
ifk(A) alt=1

for i 0 a.len
    KEYEVENT k=a[i]
    if(k.flags&0x80) i+1; continue ;;wait
    sel(k.vk)
        case [16,17,18]
        int th1=GetCurrentThreadId
        int th2=GetWindowThreadProcessId(hwnd &i)
        ati=AttachThreadInput(th1 th2 1)
        sk.all(256)
        0.01
        break

for i 0 a.len
    k=a[i]
    
    if(k.flags&0x80) ;;wait
        i+1
        opt waitmsg -1
        wait (a[i].wt/1000.0)
        continue
    
    lparam=k.sc<<16|1
    if(k.flags&KEYEVENTF_EXTENDEDKEY) lparam|0x01000000
    if(k.flags&KEYEVENTF_KEYUP) lparam|0xC0000000; up=1; else up=0
    
    if(alt) m=iif(up WM_SYSKEYUP WM_SYSKEYDOWN); lparam|0x20000000
    else m=iif(up WM_KEYUP WM_KEYDOWN)
    
    if(k.vk=VK_RETURN) 0.01 ;;some controls, eg PSPad, would insert new line too early
    
    PostMessage hwnd m k.vk lparam
    0.01
    
    sel(k.vk)
        case [16,17,18] ;;modifier keys
         wait while processes prev keys
        if(i) SendMessage hwnd 0 0 0
        0.01
        
        GetKeyboardState sk
        sk[k.vk]=iif(up 0 0x80)
        SetKeyboardState sk
    
    if(k.vk=VK_MENU) alt=!up

if(ati) ati=AttachThreadInput(th1 th2 0)

Does anyone have an idea?
#3
Search for vmware.
One of results: http://www.quickmacros.com/forum/showthr...p?tid=6215


Forum Jump:


Users browsing this thread: 1 Guest(s)