News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Sending keycodes to another program

Started by Gerhard Putschalka, February 23, 2005, 01:15:23 PM

Previous topic - Next topic

Gerhard Putschalka

In "virtualDub" I can enter the keys F6, esc, alt+F4 tostart, end the program.
How can I send these key-codes from a separate MASM32 program? I didn't find any Win-API. :red

Nilrem

Well I have never done it (and eventually wanted to ask the very same question at a later date), when you send these keys you send them to a certain handle of a window right? So couldn't you just get the handle of the application window you want to send it to? I'm not really keyed up on this, but I think it's a start.

Mark Jones

"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

doomsday

Can you find the window of this "VirtualDub" app?  From there it gets easy.

From there you've got choices.  Sending WM_CHAR message(s) to the target app will work most of the time and if not try:
hwndTemp = GetForegroundWindow();
SetForegroundWindow( hwndVirtualDub);
SendInput( xxx); // with whatever input(s) you feel are necessary
SetForegroundWindow( hwndTemp);


regards,
-Brent

Gerhard Putschalka

Hi Brent,
yes, I can get the Handle of VirtualDub, no problem. But to send the keycode to VirtualDub doesn't work.
Are you shure your example is for MAsm32?

I tried already with:  invoke SendMessage, VirtualDubHandle, WM_COPYDATA, OwnHandle, Structure
I thought this could be the function. But it doesn't work.

Cold you (or anyone) show me an Asm-example for sending a keycode (let's say for ALT+F4) to the VirtualDub-Application?

regards
Gerhard.

Robert Collins

I may be incorrect so correct me if I am but I think that not all applications are able to receive keycodes and have that application act upon them in the manner that you might expect. I have never tried to do this in MASM nor C but I have done it quite alot in VB. From my experience in VB sending keycodes to another application the recipient application must be keycode aware. Example: I write two VB programs. The first programs will send keycodes to the second but the second program has no means to interpet the keycodes so it won't do anything. If I change the second program to be keycode aware then the keycodes sent to it by the first program will work. Most Windows applications are keycode aware (those that are MS created) but third party applications may not be.

donkey

Just getting a handle to the window and sending the message may not be enough, you must also be sure that the window will accept the message. Sending a WM_CLOSE message is usually much easier than sending ALT-F4 to the window. You can also use input events to send keys directly to the keyboard input queue of the window with the focus as keypresses. For an example of how to do it you can download my system library from my website, this is the routine that sends the keypresses...

PostVKeyPress FRAME VKey,fUpDown
LOCAL kbi :KEYBDINPUT

lea eax,kbi
mov D[eax+KEYBDINPUT.dwType], INPUT_KEYBOARD
mov ecx,[VKey]
mov W[eax+KEYBDINPUT.wVk],cx
mov D[eax+KEYBDINPUT.dwFlags], 0
cmp D[fUpDown],0
je >
or D[eax+KEYBDINPUT.dwFlags],KEYEVENTF_KEYUP
:
mov D[eax+KEYBDINPUT.time], 0
mov W[eax+KEYBDINPUT.wScan], 0

invoke SendInput, 1, ADDR kbi, SIZEOF KEYBDINPUT
or eax,eax
jz >.ERROR
or eax,eax
ret
.ERROR
xor eax,eax
dec eax
RET
ENDF
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Rain Dog

I would recommend using SendMessage()

Get the HWND to the window you want to send input to using FindWindow() and send WM_CHAR message.

donkey

Yes, SendMessage is better as long as the destination window does not check to see if another process has sent the message using the InSendMessage function. Most apps do not check so it is not generally a problem, however some check the source of the message to be sure that it is internal and block ones from other processes.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

hutch--

Its not a big deal to do if you wrote both apps, a memory mapped file for the data and a broadcast send mesage to tell it to do something and you are in business. Trying it with another app is another matter, you have memory space seperation at an OS level that makes such asks difficult.

What you are looking at is a global keyboard hook so that keys simulated in one application are received at whatever app has the focus at the time.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

farrier

If you can ensure that your target application has focus:

invoke keybd_event, VK_MENU, NULL, NULL, NULL ;Send Alt key 'down'
;delay to allow target app to process key
invoke keybd_event, VK_F4, NULL, NULL, NULL ;Send F4 key 'down'
;delay to allow target app to process key
invoke keybd_event, VK_F4, NULL, KEYEVENTF_KEYUP, NULL ;Send F4 key 'up'
;delay to allow target app to process key
invoke keybd_event, VK_F4, NULL, KEYEVENTF_KEYUP, NULL ;Send Alt key 'up'


This works for me.

hth,

farrier
It is a GOOD day to code!
Some assembly required!
ASM me!
With every mistake, we must surely be learning. (George...Bush)

Gerhard Putschalka

Hi,
to avoid any confusions:

the receiving program (VirtualDub) can record AVI-Videos (a TV Card must be installed!). Start recording begins with the function-key F6, stop recording with the  escape key and end the program happens with Alt+F4.

VirtualDub is able to process these key-codes, sent by another program (I programmed it in PROFAN and it works excelent).
But I would like to make this program in MASM.

I can get the handle to the window of VirtualDub - this is not the problem

My problem is only: how can I send the string (with the key codes) to VirtualDub and how must the string be composed for these codes.

------
Thanks for your examples, I will try them. And if I will be successful I will report here.

regards
Gerhard.

Gerhard Putschalka

Hi,
I got it  :cheekygreen:
cheekygreen

Thanks to all of you.
Gerhard.

code:
;  ---- my solution for getting the handle of VirtualDub and sending the key-codes

SuNam  db  "VirtualDub",0

; get the full name and the handle of VirtualDub
WindowName Proc  ; FNameAdr:DWord,FHandle:DWord
         Local A, X, Y, Z:DWord 
         mov    FHandle,0
         mov    Z,0        ; GW_HWNDFIRST
         Op2ToOp1 X,hwnd
         .while X>0
           invoke GetWindow, X, Z
           mov  X,eax
           mov  Z,2 ; GW_HWNDNEXT
           .if  X>0
             invoke GetWindowLongA, X, -6 ; GWL_HINSTANCE
             mov Y,eax
             .if Y>0
               ; get (consecutive) the names of active applications
               invoke GetWindowText,X,addr FName, 100
               ; lookup in proc "InstrStr" for the String (in SuNam) if found in FName
               invoke InstrStr, addr SuNam,addr FName,1 
               ; if the name starts in position 1 in addr FName: name has been found, termin. loop
               .if eax==1
                 mov eax,X
                 mov FHandle,eax
                 mov  X,0  ; beende Schleife
               .endif
             .endif
           .endif
         .endw

         ; full name of application is in addr FName, handle to the application is in FHandle
         .if   eax==0
           mov FName,0 ; clear if not found (e.g. Application has not been started yet
         .endif
         ret
WindowName Endp

; and hier the part for sending Alt+F4
         invoke GetForegroundWindow  ; save handle of own window
         mov    hwndTemp,eax

         invoke SetForegroundWindow, FHandle ; activate VirtualDub-window
         ; and now simulate keyboard entries
         invoke keybd_event, VK_MENU, NULL, NULL, NULL ; Send Alt key 'down'
         invoke Warte_Zeit, 1  ; wait 1 second
         invoke keybd_event, VK_F4, NULL, NULL, NULL ; Send F4 key 'down'
         invoke Warte_Zeit, 1
         invoke keybd_event, VK_F4, NULL, KEYEVENTF_KEYUP, NULL ; Send F4 key 'up'
         invoke Warte_Zeit, 1
         invoke keybd_event, VK_F4, NULL, KEYEVENTF_KEYUP, NULL ; Send Alt key 'up'
         invoke Warte_Zeit, 1
         invoke SetForegroundWindow, hwndTemp  ; activate own window again
; --------------