• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1120
  • Last Modified:

SendKeys.Send method works in 7, not in XP SP3? Any solutions or workarounds?

Hi, I have an application that uses Sendkeys.Send method, for a reason that i don't know, this function just acts weirdly and simply does not send the key as the program goes into stackoverflow, and this only happens in Windows XP SP3, the function works perfectly in my Windows 7.<br /><br />So, any workarounds or solutions for this problem to make it working in xp too and 7 of course.<br /><br />I am using VS2010, together with .NET 4.0<br /><br />Kind Regards.<br />
0
jazzIIIlove
Asked:
jazzIIIlove
  • 7
  • 4
  • 4
  • +1
1 Solution
 
Todd GerbertIT ConsultantCommented:
I find that SendKeys is a little unreliable in general, and try to avoid it whenever possible.  What are you using it for? Maybe there's another approach that doesn't require SendKeys.

I also wouldn't expect use of SendKeys to result in a Stack Overflow condition - you might have some other problem, can you post the code for the function that you're using SendKeys in?

MSDN has some notes on forcing consistent behavior with SendKeys on different operating systems (http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx):

The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. As a result, the SendKeys class may behave differently on different operating systems. Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process.

If your application relies on consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.

<appSettings>

 <add key="SendKeys" value="SendInput"/>

</appSettings>

To force the SendKeys class to use the previous implementation, use the value "JournalHook" instead.
0
 
dj_alikCommented:
Try this: The Windows Input Simulator
http://inputsimulator.codeplex.com/
0
 
jazzIIIloveAuthor Commented:
tgerbert:
I did the change in app.config file, try every combination, no avail.

What I do is simple in fact, but still problems even editing the app.config file:
So any workarounds as my code is as follows:

In my code, in the keypress event:
if ((charKey >= Convert.ToInt16('a') && charKey <= Convert.ToInt16('z')){

       string s = e.KeyChar.ToString().ToUpper();
                                    SendKeys.Send(s);
                                    e.Handled = true;

dj_alik:
I cannot use it since there is a licensing issue.

Kind regards.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jazzIIIloveAuthor Commented:
Here is an interesting issue that when I go debug for the application, in SendKeys method, it gives this message and that option is already enabled in my VS message
0
 
jazzIIIloveAuthor Commented:
ok, the solution relies in the link:

http://www.pinvoke.net/default.aspx/user32.keybd_event

but I couldn't convert it, all I want to do is calling a function in my application that can sends keys

Could you help me?

Kind regards.
0
 
Chuck YetterCommented:
The stack overflow is probably caused by putting SendKeys inside of the keypress event.  The first keypress triggers the second which triggers the third... until overflow.  Fix that and then see if your code works.  I use SendKeys.Send() with Forms on XP SP3 with no problems.
0
 
jazzIIIloveAuthor Commented:
ok, but how to fix that? Where should I put/call the function?

Kind regards.
0
 
Chuck YetterCommented:
I'm not sure what your code looks like, but I would think you'd put the SendKeys call in a button click event handler instead of a keypress event handler.  You also have to set the focus to whatever is receiving the keystroke.
0
 
jazzIIIloveAuthor Commented:
ok, but I am getting the keyChar. How can I convert from KeyEvent to Event?

Kind regards.
0
 
Chuck YetterCommented:
If you are sending keystrokes to a different app or window, you need to set the focus to the other app or window.  Otherwise, your app will keep sending itself the keystrokes.  If the keystrokes are going to something else within your app, you still have to set the focus to the textbox or whatever it is.
0
 
Todd GerbertIT ConsultantCommented:
@jazzIIIlove

Can you 1) post the entire code for the method in question (including it's signature), and 2) describe what you're trying to achieve?
0
 
jazzIIIloveAuthor Commented:
>>If you are sending keystrokes to a different app or window, you need to set the focus to the other >>app or window.
The keystroke is within the same application. I set the focus for it but no help...
private void myGrdTreeEditorList_KeyPress(object sender, KeyPressEventArgs e) {
int chKey = (int)e.KeyChar;
if (chKey >= Convert.ToInt16('a') && chKey <= Convert.ToInt16('z')){
string s = e.KeyChar.ToString().ToUpper();
//SendKeys.SendWait(s());
SendKeys.Send(s());
SendKeys.Flush();
}
}

myGrdTreeEditorList is a component one controller as  C1TrueDBGrid, you can download it from component one's website.

Kind regads.
0
 
Chuck YetterCommented:
Where do you set the focus to the other control which receives the text?  I don't see any code for that in your event handler.
0
 
jazzIIIloveAuthor Commented:
You are right! Damn! How can I miss that??

Thank you
0
 
Todd GerbertIT ConsultantCommented:
It's not feasible to just set the ".Text" property of the control you're sending keystrokes to?
0
 
Todd GerbertIT ConsultantCommented:
Sorry I fell a little behind on this. ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now