Skip to content

Commit

Permalink
Pass SendKeys state by ref (dotnet#6711)
Browse files Browse the repository at this point in the history
Regression introduced in 90f7c1d.

Fixes dotnet#6666

Co-authored-by: Jeremy Kuhne <jkuhne@microsoft.com>
  • Loading branch information
github-actions[bot] and JeremyKuhne authored Feb 23, 2022
1 parent f7295a4 commit e9e5c8c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/System.Windows.Forms/src/System/Windows/Forms/SendKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private static bool AddSimpleKey(
}

AddMsgsForVK(vk & 0xff, repeat, haveKeys.HaveAlt > 0 && haveKeys.HaveCtrl == 0, hwnd);
CancelMods(haveKeys, UnknownGrouping, hwnd);
CancelMods(ref haveKeys, UnknownGrouping, hwnd);
}
else
{
Expand Down Expand Up @@ -205,7 +205,7 @@ private static void AddMsgsForVK(int vk, int repeat, bool altnoctrldown, IntPtr
/// Called whenever there is a closing parenthesis, or the end of a character. This generates events for the
/// end of a modifier.
/// </summary>
private static void CancelMods((int HaveShift, int HaveCtrl, int HaveAlt) haveKeys, int level, IntPtr hwnd)
private static void CancelMods(ref (int HaveShift, int HaveCtrl, int HaveAlt) haveKeys, int level, IntPtr hwnd)
{
if (haveKeys.HaveShift == level)
{
Expand Down Expand Up @@ -519,7 +519,7 @@ private static void ParseKeys(string keys, IntPtr hwnd)
}

AddMsgsForVK(vk, repeat, haveKeys.HaveAlt > 0 && haveKeys.HaveCtrl == 0, hwnd);
CancelMods(haveKeys, UnknownGrouping, hwnd);
CancelMods(ref haveKeys, UnknownGrouping, hwnd);
}
else if (keyName.Length == 1)
{
Expand Down Expand Up @@ -604,7 +604,7 @@ private static void ParseKeys(string keys, IntPtr hwnd)
throw new ArgumentException(string.Format(SR.InvalidSendKeysString, keys));
}

CancelMods(haveKeys, cGrp, hwnd);
CancelMods(ref haveKeys, cGrp, hwnd);
cGrp--;
if (cGrp == 0)
{
Expand Down Expand Up @@ -632,7 +632,7 @@ private static void ParseKeys(string keys, IntPtr hwnd)
throw new ArgumentException(SR.SendKeysGroupDelimError);
}

CancelMods(haveKeys, UnknownGrouping, hwnd);
CancelMods(ref haveKeys, UnknownGrouping, hwnd);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.Windows.Forms.Tests
{
public class SendKeysTests
{
[WinFormsFact(Skip = "This test depends on focus and should be run manually.")]
public void SendKeysGrouping()
{
// Regression https://github.com/dotnet/winforms/issues/6666

using var form = new CaptureForm();
form.Show();
form.Focus();
SendKeys.SendWait("^(a)^(c)");

Assert.Equal(4, form.KeyEvents.Count);
Assert.Equal(Keys.ControlKey, form.KeyEvents[0].KeyCode);
Assert.Equal(Keys.A, form.KeyEvents[1].KeyCode);
Assert.Equal(Keys.Control, form.KeyEvents[1].Modifiers);
Assert.Equal(Keys.ControlKey, form.KeyEvents[2].KeyCode);
Assert.Equal(Keys.C, form.KeyEvents[3].KeyCode);
Assert.Equal(Keys.Control, form.KeyEvents[3].Modifiers);
}

private class CaptureForm : Form
{
public List<KeyEventArgs> KeyEvents { get; } = new();

protected override void OnKeyDown(KeyEventArgs e)
{
KeyEvents.Add(e);
base.OnKeyDown(e);
}
}
}
}

0 comments on commit e9e5c8c

Please sign in to comment.