SendKeys and uppercase problems

SendKeys thinks I'm holding down Shift.

Page 1 of 1

6 Replies - 4070 Views - Last Post: 09 September 2009 - 06:59 AM Rate Topic: -----

#1 switchbadger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-September 09

SendKeys and uppercase problems

Posted 08 September 2009 - 06:32 AM

I'm writing an application that augments text as you type by looking for key words in what you type but when I use an Uppercase letter before an augmentation is inserted the next augmentation is written as though I've held down the Shift key. I would like this to stop.

My code is as follows:

void gkh_KeyUp(Object sender, KeyEventArgs e)
		{
			if (char.IsLetterOrDigit((char) e.KeyCode))
			{
				word += e.KeyCode.ToString().ToLower();

				if (dictionary.SmileyCheck(word))
				{
					SendKeys.Send(dictionary.GetConversion(word));
					word = "";
				}
			}
			else if (e.KeyCode == Keys.Space || e.KeyCode == Keys.Enter)
			{
				word = "";
			}
			else if (e.KeyCode == Keys.Back)
			{
				if (word.Length > 0)
					word = word.Remove(word.Length - 1);
			}
		}


If I was to augment "pie" after I type "apple" I would get:
apple pie
applE pie
apple PIE (This last line should read "apple pie")

Thanks for reading my question

Is This A Good Question/Topic? 0
  • +

Replies To: SendKeys and uppercase problems

#2 Crehl  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 49
  • Joined: 25-January 09

Re: SendKeys and uppercase problems

Posted 08 September 2009 - 07:18 AM

[Edit: After reading through your post again, I'm still not sure if I've got the right end of the stick, so I apologise if what I've written is of no use to you]

I can't tell exactly what you're trying to do, but the following list should help you:

When you type an uppercase letter using shift, this is what happens:

1) A shift key down message is sent,
2) A letter key down message is sent,
3a) A letter key up message is sent,
3b) Your application sees this letter key up, and invokes SendKeys,
4) A shift key up message is sent.

Because the application SendKeys before the shift key is up, it's interpreted as uppercase.

In short, your application needs to do one of two things:
1) Wait until after the shift key up message is sent (However, this will cause problems if other letters are pressed while shift is still held down)
2) Remember when the shiftkey is down, and send a shift key up, the augmentation, then shift key down again (Unless the SendKeys shift modifier can be used - not sure)

If I've understood what you're trying to do and what the problem is, then the above should give you a starting point to fixing it.

-Crehl

This post has been edited by Crehl: 08 September 2009 - 08:14 AM

Was This Post Helpful? 1
  • +
  • -

#3 switchbadger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-September 09

Re: SendKeys and uppercase problems

Posted 09 September 2009 - 12:03 AM

Thanks for the reply! That was exactly the kind of thing I needed!

I only have a two questions left...
What's the difference between Keys.Shift and Keys.ShiftKey? All I know is the word "modifier" but that doesn't mean anything to me.
AND
I'm trying to persuade my machine that while it's inserting the augmentation I'm not pressing the shift key with this code
 //Turn off the shift key
if (Control.ModifierKeys == Keys.Shift)
{
	byte[] keyStates = new byte[255];
	GetKeyboardState(keyStates); //load the keyboard
	keyStates[16] = 0; // turn off the shift key
	SetKeyboardState(keyStates); //set the new keyboard state
	shifty = true;
}
SendKeys.Send(dictionary.GetConversion(word));
word = "";
//Turn the shift key back on
if (shifty)
{
	byte[] keyStates = new byte[255];
	GetKeyboardState(keyStates); //load the keyboard
	keyStates[16] = 1; // turn on the shift key
	SetKeyboardState(keyStates); //set the new keyboard state
	shifty = false;
}


but it's still not convinced. I've also tried unhooking my keyboard from keyboard hook while it does the SendKeys but to no avail.

This post has been edited by switchbadger: 09 September 2009 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 Crehl  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 49
  • Joined: 25-January 09

Re: SendKeys and uppercase problems

Posted 09 September 2009 - 02:55 AM

I'm not entirely sure on the Shift/ShiftKey situation. I think ShiftKey refers to pressing the actual shift key itself (So before a letter is pressed), while Shift (As a modifier) is sent when the shift key is held down and a letter is pressed. MSDN has an article here which may shed some light on it.

Either it's not detecting the shift key (And therefore not doing anything about it, which would be my guess) or it's detecting it but not setting it. Have you run any tests to check that it's able to simulate the shift key being pressed/unpressed?

However, I would guess that it's not even detecting the shift key. I've not used Control.ModifierKeys, so I can't really tell if that's the issue. I would probably try the following instead, and see if you get results that way:
if (e.Shift)
{
	//...
}

SendKeys.Send(dictionary.GetConversion(word));
//...


I don't often use SendKeys or interact with key press event handlers, so it may be another issue; in which case I hope someone knows more about it than I do.

Once again, I hope that's some help,
-Crehl

N.B. One final thought - have you tried testing this with caps lock on? It could cause the same problem, and it should be simple enough to solve once you've got the shift key sorted out.
Was This Post Helpful? 0
  • +
  • -

#5 switchbadger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-September 09

Re: SendKeys and uppercase problems

Posted 09 September 2009 - 03:44 AM

Thanks for the article. Will read that in a bit.

I thought I did the testing but when I went back and did it again I obviously proved myself wrong. (Must stop pulling all nighters) You were right in that the if statements were always evaluating to false. I removed them to just always override the fact that I'm holding down the Shift key or not and now I see that it's also not setting it!

byte[] 
keyStates = new byte[255];
GetKeyboardState(keyStates); //load the keyboard
keyStates[16] = 0; // turn off the shift key
SetKeyboardState(keyStates); //set the new keyboard state
SendKeys.Send(dictionary.GetConversion(word));
word = "";
//Turn the shift key back on
GetKeyboardState(keyStates); //load the keyboard
keyStates[16] = 1; // turn on the shift key
 SetKeyboardState(keyStates); //set the new keyboard state


This still gives me my old problem of that augmentation being capitalised. The strange part is that you would expect
apple pie
APPLE PIE
apple pie

but I'm getting
apple pie
APPLE pie
apple PIE

As for the CAPS lock test, I have run it through and saw a similar issue but I will use what ever technique I learn for the shift on that too.

Thanks for your input! You have greased the mental gears somewhat!
Was This Post Helpful? 0
  • +
  • -

#6 Crehl  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 49
  • Joined: 25-January 09

Re: SendKeys and uppercase problems

Posted 09 September 2009 - 05:10 AM

I presume the GetKeyboardState and SetKeyboardState method calls are referring to the ones in user32.dll? If so, there's your problem:

Quote

http://msdn.microsof...314(VS.85).aspx
The SetKeyboardState function copies a 256-byte array of keyboard key states into the calling thread's keyboard input-state table. This is the same table accessed by the GetKeyboardState and GetKeyState functions. Changes made to this table do not affect keyboard input to any other thread.


Which means that using SetKeyboardState to simulate the shift key will only alter input (Such as whether or not the shift key is pressed) for the thread that calls it. From what I gathered, you need it to work for all applications.

SendKeys can be fooled into pressing and releasing the shift key using "+() Text", but this causes problems with caps lock and when the user continues typing afterwards. SendInput (user32.dll) may be a solution, but it's not something I've ever used. The PInvoke.net page for SendInput is here. You might have to search around for articles and examples for it, and then go from there.

Keep those mental gears working!
Was This Post Helpful? 1
  • +
  • -

#7 switchbadger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-September 09

Re: SendKeys and uppercase problems

Posted 09 September 2009 - 06:59 AM

Sweet! That neatly describes the problem I've been having!

I'll get right on that. Thanks very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1