AutoHotkey Community

It is currently May 26th, 2012, 9:54 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: October 9th, 2008, 1:48 am 
Offline

Joined: July 21st, 2006, 12:26 am
Posts: 223
[ description ]
It sends characters or "emulate" physical key input, converting and sending the text through postmessage.

Usage:
Code:
xpostmsg("text",MSG,WinTitle)

Please read the example section (at bottom) and the following notes:

0x102 corresponds to WM_CHAR. Its possible to use 0x100, which is WM_KEYDOWN, to emulate physically key down press. If you opt to use it, note that only [a-z],[0-9] characters will work for the text conversion. For the other symbols, use as reference: http://msdn.microsoft.com/en-us/library/ms646280.aspx

[ function ]
Code:
xpostmsg(text,msg,id) {
   if (msg = "0x100")
      stringupper text, text
   setformat, integer, h
   loop, parse, text
      postmessage, msg, asc(a_loopfield), , , %id%
}


[ example ]
Usage:
After loading the script, choose a field of input (eg: notepad) clicking on it, then press F1. Once F1 is pressed, the window can be hidden and the script will keeping sending the message (like controlsend behaviour)
Code:
#NoEnv
#SingleInstance Force
#Persistent
Critical,On
SetBatchLines,-1
SetControlDelay,-1
SetKeyDelay,-1
DetectHiddenWindows, On
Thread, NoTimers
Process, Priority, , R
SetTitleMatchMode Regex

xpostmsg(text,msg,id) {
   if (msg = "0x100")
      stringupper text, text
   setformat, integer, h
   loop, parse, text
      postmessage, msg, asc(a_loopfield), , , %id%
}

*F1::
VarSetCapacity(GuiThreadInfo, 48)
NumPut(48, GuiThreadInfo, 0)
if DllCall("GetGUIThreadInfo", uint, 0, str, GuiThreadInfo)
   HWND := NumGet(GuiThreadInfo, 12)
   
identity .= "ahk_id " . HWND

xpostmsg( "this is a test: ?%~¨*http://``"" <- don't forget to double ", 0x102, identity )
return


Thanks Titan for the loop fix :)

_________________
                                  [ profile | ahk.net | ahk.talk ]


Last edited by k3ph on October 9th, 2008, 5:04 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 9th, 2008, 9:41 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Thanks, this will be very useful to me. I've simplified your function a bit:

Code:
xpostmsg(text, msg, id) {
   Loop, Parse, text
      PostMessage, msg, Asc(A_LoopField), , , %id%
}

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 10th, 2008, 5:12 am 
Offline

Joined: July 21st, 2006, 12:26 am
Posts: 223
ive write this for lazy people and for the newcomers...
not for u titanz, go finish rusty common'! :P

_________________
                                  [ profile | ahk.net | ahk.talk ]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 10th, 2008, 8:28 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
What are the benefits of this over ControlSend? I guess sending WM_CHAR may work in some instances where ControlSend doesn't, since ControlSend only sends WM_KEYDOWN and WM_KEYUP. Otherwise, I would think ControlSend would be easier and more efficient. (Did you know it uses PostMessage internally?)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 10th, 2008, 5:57 pm 
Offline

Joined: July 21st, 2006, 12:26 am
Posts: 223
1- some inputs don't need WM_KEYUP (it would cut some time and would be theorically faster than controlsend), forcing the user have more control over the WM.
2- controlsend can't hold key presses for a long time
3- more control over keyinput order, don't need to sleep
4- controlsend can't send mouse clicks

_________________
                                  [ profile | ahk.net | ahk.talk ]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 11th, 2008, 2:40 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
k3ph wrote:
1- some inputs don't need WM_KEYUP (it would cut some time and would be theorically faster than controlsend), forcing the user have more control over the WM.
Your theory doesn't take into account the overhead of script. The script may be "much" slower, although the target window might spend less time processing the messages. Then again, if the target doesn't need WM_KEYUP, it isn't likely to take long processing it.
Quote:
2- controlsend can't hold key presses for a long time
What does your script offer that {key down} .. {key up} does not?
Quote:
3- more control over keyinput order, don't need to sleep
Could you give an example? I don't see how your script has more control than ControlSend, why ControlSend would need sleep, or how your script removes or satisfies this need.
Quote:
4- controlsend can't send mouse clicks
Your script cannot mix key-presses and mouse clicks, so it would be logical to include ControlClick in the comparison.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 11th, 2008, 3:44 am 
Offline

Joined: July 21st, 2006, 12:26 am
Posts: 223
Lexikos wrote:
Your theory doesn't take into account the overhead of script. The script may be "much" slower, although the target window might spend less time processing the messages. Then again, if the target doesn't need WM_KEYUP, it isn't likely to take long processing it.

So are you alleging that ControlSend and Postmessage would have the same effect? Experimentally postmessage benchmarks doesn't shows that. There is even a slightly difference between sendmessage and postmessage (it shouldn't have such difference, don't ask me why, go test it).

Lexikos wrote:
What does your script offer that {key down} .. {key up} does not?

Users want to hold keys like ctrl, alt, shift, space for long time and press simultaneasly other keys, ControlSend can't handle that. Even with {key down}{key up} ControlSend can't hold keys for a long time. Try it by yourself and see the results, users have been reporting this a long time and no one cared about it.

Lexikos wrote:
Could you give an example? I don't see how your script has more control than ControlSend, why ControlSend would need sleep, or how your script removes or satisfies this need.

When you input a lot of text data through controlsend, if you try to send {enter} after them, it would require a sleep because the enter key is sent before the text message completes. This problem was reported by some users too.

Lexikos wrote:
Your script cannot mix key-presses and mouse clicks, so it would be logical to include ControlClick in the comparison

PostMessage is a command with more broad usage capability, there is nothing illogical on that.

_________________
                                  [ profile | ahk.net | ahk.talk ]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 11th, 2008, 4:41 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
k3ph wrote:
So are you alleging that ControlSend and Postmessage would have the same effect?
Since you ask, I assume you didn't know ControlSend uses PostMessage. It posts WM_KEYDOWN and WM_KEYUP messages.
Quote:
There is even a slightly difference between sendmessage and postmessage (it shouldn't have such difference, don't ask me why, go test it).
PostMessage does not need to wait for the target window to process the message. SendMessage does.
Quote:
Users want to hold keys like ctrl, alt, shift, space for long time and press simultaneasly other keys, ControlSend can't handle that.
Perhaps for modifier keys this is the issue:
the help file wrote:
By default, modifier keystrokes (Control, Alt, Shift, and Win) are sent as they normally would be by the Send command. This allows command prompt and other console windows to properly detect uppercase letters, control characters, etc. It may also improve reliability in other ways.

However, in some cases these modifier events may interfere with the active window, especially if the user is actively typing during a ControlSend or if the Alt key is being sent (since Alt activates the active window's menu bar). This can be avoided by explicitly sending modifier up and down events as in this example:
ControlSend, Edit1, {Alt down}f{Alt up}, Untitled - Notepad

For other keys, ControlSend posts WM_KEYDOWN/WM_KEYUP messages. In theory, the only way it could fail is in the target window's processing of the messages/handling of keyboard state.
k3ph wrote:
users have been reporting this a long time and no one cared about it
It's the first I've heard about it.
Quote:
When you input a lot of text data through controlsend, if you try to send {enter} after them, it would require a sleep because the enter key is sent before the text message completes.
ControlSend doesn't post the message for a key until after the message for a previous key has been posted. Therefore, I suspect the target window's handling of the messages.
Quote:
PostMessage is a command with more broad usage capability, there is nothing illogical on that.
I was pointing out that your function can send only a single type of message with any given call. If it could send both keystrokes and mouse clicks in a single call, that may be an advantage over ControlSend/ControlClick. Since it can't, your argument that ControlSend can't click is pointless, as a user can easily ControlClick.


The main reason for my posts is that you say the function is intended for lazy people and newcomers, but I cannot see how it could be easier, quicker, more intuitive or more effective. I say lazy people and newcomers are better off sticking with the built-in methods.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group