Page 1 of 1

Keypress using Sendinput_() Not always working.

Posted: Thu Feb 08, 2024 1:53 am
by matalog
Using this program, I can 'type the letters "c" and "C" into notepad++, MS Word, internet explorers etc, but it doesn't make all programs think a key is being pressed.

Code: Select all

;  Taken from a program by Axeman - https://www.purebasic.fr/english/viewtopic.php?p=579335

Global G_target_window_handle

Global G_multi_tab_delay = 100
Global G_multi_tab_back_delay = 100

Global NewList WindowList() ; Used to store a list of window handles.

Global Dim KeyArray.INPUT( 0 )
Global *G_key.INPUT = @KeyArray( 0 )
*G_key\type = #INPUT_KEYBOARD

Dim lets.s(2,128)



Procedure SendNormalKey( keycode )
			*G_key\ki\wVk = keycode
			*G_key\ki\dwFlags = 0 ; 0 for key press.
			SendInput_( 1, KeyArray(), 40 )
			
			*G_key\ki\wVk = keycode
			*G_key\ki\dwFlags = #KEYEVENTF_KEYUP ; #KEYEVENTF_KEYUP for key release.
			SendInput_( 1, KeyArray(), 40 )
EndProcedure


Procedure SendNormalKeys( keycode, keycode2 )
  		*G_key\ki\wVk = keycode2
			*G_key\ki\dwFlags = 0 ; 0 for key press.
			SendInput_( 1, KeyArray(), 40 )
			
			*G_key\ki\wVk = keycode
			*G_key\ki\dwFlags = 0 ; 0 for key press.
			SendInput_( 1, KeyArray(), 40 )
			
			*G_key\ki\wVk = keycode
			*G_key\ki\dwFlags = #KEYEVENTF_KEYUP ; #KEYEVENTF_KEYUP for key release.
			SendInput_( 1, KeyArray(), 40 )
			
			*G_key\ki\wVk = keycode2
			*G_key\ki\dwFlags = #KEYEVENTF_KEYUP ; #KEYEVENTF_KEYUP for key release.
			SendInput_( 1, KeyArray(), 40 )
		EndProcedure
		


SendNormalKey(Asc("C"))
SendNormalKeys(Asc("C"),160)

I have some emulators that do not respond to Sendinput_(). Any ideas on another method that may work with programs that allow keyboard entry, but which do not respond to Sendinput()?

Re: Keypress using Sendinput_() Not always working.

Posted: Thu Feb 08, 2024 2:29 am
by Demivec
What emulators?

Re: Keypress using Sendinput_() Not always working.

Posted: Thu Feb 08, 2024 2:38 am
by matalog
Various ZX Spectrum emulators. All 3 that I have tried. FUSE is one of them.


Actually, thinking about it now, I probably need to set a delay between 'pressing' the key and 'releasing' it, as the keys are scanned only every 50th of a second in a Spectrum, and probably in the emulator too, with no buffer.

I'll give that a go tomorrow.

Re: Keypress using Sendinput_() Not always working.

Posted: Thu Feb 08, 2024 10:31 am
by matalog

Code: Select all

;  Taken from a program by Axeman - https://www.purebasic.fr/english/viewtopic.php?p=579335

Global G_target_window_handle

Global G_multi_tab_delay = 100
Global G_multi_tab_back_delay = 100

Global NewList WindowList() ; Used to store a list of window handles.

Global Dim KeyArray.INPUT( 0 )
Global *G_key.INPUT = @KeyArray( 0 )
*G_key\type = #INPUT_KEYBOARD

Dim lets.s(2,128)



Procedure SendNormalKey( keycode )
			*G_key\ki\wVk = keycode
			*G_key\ki\dwFlags = 0 ; 0 for key press.
			SendInput_( 1, KeyArray(), 40 )
			
			*G_key\ki\wVk = keycode
			*G_key\ki\dwFlags = #KEYEVENTF_KEYUP ; #KEYEVENTF_KEYUP for key release.
			SendInput_( 1, KeyArray(), 40 )
EndProcedure


Procedure SendNormalKeys( keycode, keycode2 )
  		*G_key\ki\wVk = keycode2
			*G_key\ki\dwFlags = 0 ; 0 for key press.
			SendInput_( 1, KeyArray(), 40 )
			
			*G_key\ki\wVk = keycode
			*G_key\ki\dwFlags = 0 ; 0 for key press.
			SendInput_( 1, KeyArray(), 40 )
			
			*G_key\ki\wVk = keycode
			*G_key\ki\dwFlags = #KEYEVENTF_KEYUP ; #KEYEVENTF_KEYUP for key release.
			SendInput_( 1, KeyArray(), 40 )
			
			*G_key\ki\wVk = keycode2
			*G_key\ki\dwFlags = #KEYEVENTF_KEYUP ; #KEYEVENTF_KEYUP for key release.
			SendInput_( 1, KeyArray(), 40 )
		EndProcedure
		


SendNormalKey(Asc("C"))
SendNormalKeys(Asc("C"),160)


I was going to test more with this at work, where I have the latest demo of PB installed, and it doesn't run, returning "Line 10: Structure not found INPUT"

It runs fine on full version.

How can this be avoided?

Re: Keypress using Sendinput_() Not always working.

Posted: Thu Feb 08, 2024 6:58 pm
by matalog
So, even giving half a second delay between keyon and key off, the SendInput_() doesn't work with any of my ZX Spectrum emulators.

And ideas of another key 'simulation' methods?