MS SendInput

The open discussion of hosting, forum, web-sites, community organisation and open voting
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: MS SendInput

Post by Josef Templ »

DGDanforth wrote: Some procedures do and others don't use [ccall]
-Doug
OK, there is one exception in WinApi, wsprintf.
It uses ccall because it is defined to use ccall in the corresponding C header file.
This is also documented in msdn.
This function is pretty useless, by the way, because it lacks the vararg parameter. A vararg
cannot be expressed in CP. In C a vararg is a variable number of arguments.
This function also has severe security issues.
It would be a candidate for removal, I think.

- Josef
User avatar
DGDanforth
Posts: 59
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, CA, U.S.A.
Contact:

Re: MS SendInput

Post by DGDanforth »

The test program of SendInput works as advertized.
However, it does not work in the context I am using.
I am attempting to send keys to a different process.

I can do that easily using the deprecated function
WinApi.keybd_event
I attach to the process and set its focus.

So why do I need SendInput?
I was hoping it would resolve timing issues of keystroke handling.
Currently with WinApi.keybd_event I need to experimentally determine
the time between a keydown and a keyup (about 65ms). But that should
not be necessary (I believe). I would think that Microsoft and SendInput
would know whether an application is ready to receive input and wait
until it is then send a key. That doesn't seem to be the case. When I
use SendInput it does not fail and yet returns immediately even though
I have specified more than 500 keystrokes.

What must I do to get SendInput to work?
-Doug
User avatar
DGDanforth
Posts: 59
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, CA, U.S.A.
Contact:

Re: MS SendInput

Post by DGDanforth »

I discovered that SendInput sends all INPUT records simultaneously (it doesn't wait after sending one INPUT).
That is not what I want.
So, I have broken the array of inputs into individual INPUT and send them sequentially (with a delay).
That still doesn't work for sending to another process.

So I am back using the obsolete function and that works most of the time.
I have not yet found how to delay sending based on whether the process has eaten the previous characters I have sent.
PeekMessage always says there is nothing waiting.

-Doug
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: MS SendInput

Post by Josef Templ »

Maybe it is required to send both, key-down and key-up events.
Are you sending both?

- Josef
User avatar
DGDanforth
Posts: 59
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, CA, U.S.A.
Contact:

Re: MS SendInput

Post by DGDanforth »

Josef Templ wrote:Maybe it is required to send both, key-down and key-up events.
Are you sending both?

- Josef
Yes, first down and then up.

I have also tried SendMessage since it claims to wait until the message is eaten.
Again, I use both down and up but that does not seem work.

Only

Code: Select all

	PROCEDURE KeyDown (key: Key);
	VAR 
		bScan: SHORTCHAR;
		dwFlags: SET;
		dwExtraInfo: INTEGER;
		tkey:	Key;
	BEGIN
		tkey :=TranslateKey(key);
		bScan := 0X; (*SHORT(CHR(WinApi.MapVirtualKey(ORD(tkey), 0)));*)
		dwFlags := {};
		dwExtraInfo := 0;
		WinApi.keybd_event (tkey, bScan, dwFlags, dwExtraInfo)
	END KeyDown;
	
	PROCEDURE KeyUp (key: Key);
	VAR 
		bScan: SHORTCHAR;
		dwFlags: SET;
		dwExtraInfo: INTEGER;
		tkey:	Key;
	BEGIN
		tkey :=TranslateKey(key);
		bScan := 0X; (*SHORT(CHR(WinApi.MapVirtualKey(ORD(tkey), 0)));*)
		dwFlags := WinApi.KEYEVENTF_KEYUP;
		dwExtraInfo := 0;
		WinApi.keybd_event (tkey, bScan, dwFlags, dwExtraInfo)
	END KeyUp;
works (most of the time). TranslateKey simply maps "R" to a right move key, etc.
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: MS SendInput

Post by Josef Templ »

with SendInput there is the requirement that the other process is at the same security level,
whatever that means in detail, but this is documented in msdn.
Maybe that is the problem why SendInput does not work.

- Josef
User avatar
DGDanforth
Posts: 59
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, CA, U.S.A.
Contact:

Re: MS SendInput

Post by DGDanforth »

Josef Templ wrote:with SendInput there is the requirement that the other process is at the same security level,
whatever that means in detail, but this is documented in msdn.
Maybe that is the problem why SendInput does not work.

- Josef
Yes, I noticed that.
I am not sure how to determine the security level (have you yet looked into the issue)
- Doug
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: MS SendInput

Post by Josef Templ »

I can use SendInput to send a keystroke to another BlackBox process.
At least in this case sending a key-down is sufficient.
Check if the receiving process has the focus.

- Josef
User avatar
DGDanforth
Posts: 59
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, CA, U.S.A.
Contact:

Re: MS SendInput

Post by DGDanforth »

Josef Templ wrote:I can use SendInput to send a keystroke to another BlackBox process.
At least in this case sending a key-down is sufficient.
Check if the receiving process has the focus.

- Josef
to another BlackBox process
In my case the other process is a game.
Yes, it has the focus.
Let me try it again with SendInput.
- Doug
User avatar
DGDanforth
Posts: 59
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, CA, U.S.A.
Contact:

Re: MS SendInput

Post by DGDanforth »

Josef,
The game does not respond even when I put a 500ms delay between the down and the up.
The game has the focus when I am sending.

Code: Select all

WinApi.Sleep(sleepTime);
SendInput returns a non zero result indicating success.
-Doug
Post Reply