473,416 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

queue & replay keyboard messages in a win form instead of using SendKeys.Send()

I'm buidling some simple macro functionality for my app so the users can record a sequence of keyboard inputs and replay them reliably via some menu.

Originally, I used:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
To map "Keys" objects to their string constant, and stored them on a string Queue as they were happening. Then (after resetting the form), I simulate replaying of these actions by flushing the queued strings and used: SendKeys.Send(string keys); to replay the keystrokes.

Example: assuming 2 textboxes are in a form, if you execute
SendKeys.Send("abc");
SendKeys.Send("{TAB}");
SendKeys.Send("123");

You end up with "abc" in the first textbox, then "123" in the second one. Nice!

This works but has two flaws:
-I need to keep maping the keydata parameter of ProcessCmdKey to a string, whenever I get a Keys object I haven't mapped.
-At some point I will have to do mouse events, and this approach won't cut it. (however I don't care about that much right now).

My second approach was to queue windows messages, and thats where my problem/question arises. If I queue (into some data structure) windows keyboard messages as they arrive to the form

private Queue<Message> mq = new Queue<Message>();
private bool flushing = false;
//QUEUE MESSAGES AS SEEN BY FORM. Note the flushing member prevents infinite queuing.
protected override bool ProcessKeyPreview(ref Message m)
{

if (!flushing)
{
Debug.WriteLine("enq");
mq.Enqueue(m);

}
return base.ProcessKeyPreview(ref m);
}


and then use the win32 call to flush my queue


[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

//FLUSH THE QUEUE BY SENDING THE MESSAGES BACK TO THE WINDOW PROC OF THE FORM.
private void FlushQ()
{
Debug.WriteLine("flushing q " + mq.Count.ToString());
flushing = true;
while (mq.Count > 0)
{
Message m = mq.Dequeue();
SendMessage(new HandleRef(this, this.Handle), (uint)m.Msg, m.WParam, m.LParam);

}

flushing = false;
}

The flushQ function "seems" to work because I do see the messages flowing again into ProcessKeyPreview. But that's it. The textboxes don't get the text. I suspect the form never passes the message to the child controls, so if instead I do

SendMessage(new HandleRef(this.textBox1, this.textBox1.Handle), (uint)m.Msg, m.WParam, m.LParam);

then only the textbox1 will get the messages. That's no good either because 1) the receiver of the message should really be the control with the focus after an arbitrary number of dequeued steps. 2) My queueing doesn't seem to work for special keys like TAB, where the control should change focus.

I suspect I'm just not queuing things at the right place, and I don't know how to replay them properly so the messages go to the control that has the input focus (I could keep track of who has the input focus by handling the "enter" event, but I rather use something more reliable). I tried queuing things at WndProc but...I can't make sense of the sheer number of messages going there. Some should be ignored, some not and I honestly don't know which ones. Either way I can't replay them.

Am I totally wrong in my approach to save and replay keyboard messages? How does SendKeys.Send() get it right? I'll admit I'm new to windows message processing so any help/insight solving this problem would be appreciated. Disregard how to actually serialize a message queue to disk. And if you know of a way to get the mouse queuing properly while you're at it, great but I don't care about mouse input too much.
Thanks.
Dec 31 '06 #1
0 2704

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Robert Rossney | last post by:
I'm trying to send and receive XmlDocument objects using the System.Messaging.dll functions. The code I've written follows, as best I can tell, the methodology used in the sample code for the...
4
by: Ralf Toender | last post by:
Hi! Does anybody know how to send a keystroke C#? In C++ it's done by: keybd_event ( ... ) or SendInput ( ... ). What namespace does include this counterpart? Thanks Ralf
1
by: tiger | last post by:
Hi, I have a question on Microsoft Message Queue. How do I give higher priority to messages and get it process before others. I will like to use the mechanism supplied by the queue service...
1
by: newcomer | last post by:
I have traced action in XML. Now I would like to replay that. How could I do that based on this XML trace? I am using soap:tcp between client and server. thanks,
7
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int...
2
by: Charlie Brown | last post by:
I can open an application such as Notepad and use SendKeys to send input to the Notepad. What I would like to do is reference an already running application and then send keyboard input to it. ...
3
by: Paulers | last post by:
Hello, I need to emulate keyboard strokes from a console application. The console application monitors a textfile and when something is matched in a text file I need the matched string outputted...
4
by: davermcl | last post by:
Hi, I'm experiencing a problem when using the VB SendKeys method. I'm sending characters to a textbox in another application. It works fine when the Windows Input Languages on both apps are...
1
by: zanthor | last post by:
Ok... so call me crazy, but I have an idea and I'm looking for where to start... I want to write an application that catches keystrokes and then either redirects them to the in focus window or...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.