Page 1 of 1
[Solved] PostMessage / #WM_KEYDOWN / #WM_KEYUP
Posted: Tue Oct 23, 2012 12:05 pm
by TO7
Hi,
I have two problem with this code:
1 / Apparently the # WM_KEYUP double the character, is this normal?
2 / I can not write the numbers 1 and 2, they are replaced by the symbol that is on the same key.
I'm sending the SHIFT key, it still does not work
Could you help me?
Code: Select all
RunProgram("notepad.exe")
Delay(1000)
hwnd = FindWindow_(0,"Sans titre - Bloc-notes")
If hwnd
Hwndedit = FindWindowEx_(hwnd, 0, "edit", 0)
a$ = "Hello 1"+ #CR$ + "Hello 2"
For i = 1 To Len(a$)
vk$ = Mid(a$, i, 1)
vk = VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(0)) ; Normal key found
PostMessage_(Hwndedit, #WM_KEYDOWN, #VK_SHIFT, 0);
Delay(10)
PostMessage_(Hwndedit, #WM_KEYDOWN, vk, 0);
Delay(10)
PostMessage_(Hwndedit, #WM_KEYUP, vk, 0)
Delay(10)
PostMessage_(Hwndedit, #WM_KEYUP, #VK_SHIFT, 0);
Delay(10)
Next
Else
MessageRequester("Error", "Not found NotePad")
EndIf
thank you
Re: PostMessage / #WM_KEYDOWN / #WM_KEYUP
Posted: Tue Oct 23, 2012 1:50 pm
by luis
PostMessage is not the right way to simulate keyboard input to another app, some reasons are explained here:
http://blogs.msdn.com/b/oldnewthing/arc ... 23202.aspx
You should use SendInput().
Or sometimes you can simply use keybd_event() even if it was replaced by SendInput()
I have some macros using keybd_event() so here it is your code changed to using them.
For SendInput() search in the forum for many advanced examples made by others.
Code: Select all
Enumeration
#PUSH_CTRL = 0
#PUSH_ALT = 1
#PUSH_SHIFT = 2
EndEnumeration
Macro KEY_DOWN (iKey)
keybd_event_ (iKey, 0, 0, 0)
EndMacro
Macro KEY_UP (iKey)
keybd_event_(iKey, 0, #KEYEVENTF_KEYUP, 0)
EndMacro
Procedure PushKey (iKey, iMod = 0)
If iMod & #PUSH_CTRL
KEY_DOWN (#VK_CONTROL)
EndIf
If iMod & #PUSH_SHIFT
KEY_DOWN (#VK_SHIFT)
EndIf
If iMod & #PUSH_ALT
KEY_DOWN (#VK_MENU)
EndIf
If iKey
KEY_DOWN (iKey)
KEY_UP (iKey)
EndIf
If iMod & #PUSH_ALT
KEY_UP (#VK_MENU)
EndIf
If iMod & #PUSH_SHIFT
KEY_UP (#VK_SHIFT)
EndIf
If iMod & #PUSH_CTRL
KEY_UP (#VK_CONTROL)
EndIf
EndProcedure
RunProgram("notepad.exe")
Delay(1000)
;hwnd = FindWindow_(0,"Sans titre - Bloc-notes")
hwnd = FindWindow_(0,"Untitled - Notepad")
If hwnd
Hwndedit = FindWindowEx_(hwnd, 0, "edit", 0)
a$ = "Hello 1"+ #CR$ + "Hello 2"
For i = 1 To Len(a$)
vk$ = Mid(a$, i, 1)
vk = VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(0)) ; Normal key found
PushKey (vk)
Delay(10)
Next
Else
MessageRequester("Error", "Not found NotePad")
EndIf
Re: PostMessage / #WM_KEYDOWN / #WM_KEYUP
Posted: Tue Oct 23, 2012 2:05 pm
by TO7
Thanks luis
PostMessage is not the right way to send input to another app
I try to use PostMessage because in a forum C, a member say not use SendMessage replace it by PostMessage
As you say not to use PostMessage, I wonder if you can do it with SendMessage, or in both cases should not do ?
Regarding methods of "KeyBEvent" and "SendInput" I found how.
It has been several days that I try to incorporate all methods to send a text in an application External for my generator SNIPPET
http://www.purebasic.fr/english/viewtop ... 05#p394105
I miss the SendMessage method if possible of course

Re: PostMessage / #WM_KEYDOWN / #WM_KEYUP
Posted: Tue Oct 23, 2012 2:10 pm
by luis
No, both are the same thing, one it's stuffed in a queue and the other it's executed immediately, but the problem with both it's that a message it's not enough to simulate a full key event, the messages are only part of what a keypress does.
Re: PostMessage / #WM_KEYDOWN / #WM_KEYUP
Posted: Tue Oct 23, 2012 2:14 pm
by TO7
Thanks a lot for your explanation luis

So, I mourn the loss of these two methods
So i believe i have found all methods now ??
All this methods works fine
1/ KeybEvent key by key method
2/ ClipBoard KeybEvent CTRL c+v
3/ ClipBoard Sendmessage WM_PASTE method
4/ SendInput method
5/ Sendmessage WM_CHAR method
Re: PostMessage / #WM_KEYDOWN / #WM_KEYUP
Posted: Tue Oct 23, 2012 2:23 pm
by RASHAD
Try
Code: Select all
For i = 1 To Len(a$)
vk$ = Mid(a$, i, 1)
vk = VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(0)) ; Normal key found
PostMessage_(Hwndedit, #WM_KEYDOWN, #VK_SHIFT, 0)
Delay(10)
PostMessage_(Hwndedit, #WM_KEYUP, vk, 0)
Delay(10)
Next
Re: PostMessage / #WM_KEYDOWN / #WM_KEYUP
Posted: Tue Oct 23, 2012 3:16 pm
by TO7
It's better but not again done

The Uppercase are not here, and i have not the number but the letter under it
This is the result
hello &
hello é
Re: PostMessage / #WM_KEYDOWN / #WM_KEYUP
Posted: Tue Oct 23, 2012 3:34 pm
by RASHAD
Sorry for that
Try
Code: Select all
For i = 1 To Len(a$)
vk$ = Mid(a$, i, 1)
vk = VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(0)) ; Normal key found
SendMessage_(Hwndedit,#WM_CHAR,Asc(vk$),0)
Next
Re: PostMessage / #WM_KEYDOWN / #WM_KEYUP
Posted: Tue Oct 23, 2012 4:46 pm
by TO7
Works good now
But like say luis, PostMessage not works with #WM_KEYDON/UP
it is necessary to use SendMessage and WM_CHAR
Thanks you both, i believe 5 methods to sending text is enough for the user
Best regards