0

I need to automate a client's old legacy code without touching it. This requires human input to press F17 and I want to include that key press in a longer process using Windows Forms's SendKey.Send Method to press the button. A problem arises: SendKeys.Send only supports function keys up to F16.

Looking at the source code, adding the KeywordVk for F17 at line 68 would solve the problem, but I cannot touch the .NET source code, nor inherit the class (due to the private constructor) or make a custom version of it (due to Interop not being importable).

Djarrah
  • 37
  • 6
  • 1
    Unfortunately this is really a stopper in the library. After some search I found [this project](https://github.com/trksyln/CSInputs/blob/main/NativeMethods/User32.cs) that uses the Win32 method directly. Maybe you can take a look at this and implement your own method to send some keys. – Oliver Feb 23 '23 at 10:35
  • 1
    Do you *only* have to send an F17 keypress and nothing else? What `SendKeys` does isn't magic; cobbling together a single [`SendInput`](https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-sendinput) call isn't hard. – Jeroen Mostert Feb 23 '23 at 10:43
  • "adding the KeywordVk for F17 at line 68 would solve the problem," - The `SendsKeys.keywords` field is static. You could use reflection to copy and replace it with a new array of `KeywordVk` items that includes a F17 item. But as @JeroenMostert said, using SendInput is not that hard. – TnTinMn Feb 23 '23 at 15:26
  • @TnTinMn: as the field is `readonly` I'd consider this fairly dangerous. I'm not fully clear on what can go wrong when you twiddle a non-public `readonly` field (as in, what parts of the runtime and/or the code assume the field doesn't change) -- possibly nothing, but it's dubious enough that it'd give me pause. – Jeroen Mostert Feb 23 '23 at 15:33
  • @JeroenMostert, where did you see the `readonly` modifier on [keywords](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/SendKeys.cs,7f418f7116a99f50,references) field? As far as modifying readonly fields goes, there is [some discussion here](https://stackoverflow.com/questions/934930/can-i-change-a-private-readonly-field-in-c-sharp-using-reflection). – TnTinMn Feb 23 '23 at 18:26
  • @TnTinMn: I saw it [here](https://github.com/dotnet/winforms/blob/main/src/System.Windows.Forms/src/System/Windows/Forms/SendKeys.cs). I suppose it depends on whether you're using Framework or Core; the OP didn't specify (the "old legacy code" obviously won't be Core, but that's not what the question's about). – Jeroen Mostert Feb 23 '23 at 19:25

1 Answers1

0

Solved: I used a combination between user32.dll's SetForegroundWindow and an external package called CSInputs to send inputs to the active window.

Djarrah
  • 37
  • 6