fix cross-app modifier key states by btarb24 · Pull Request #5209 · ScottPlot/ScottPlot · GitHub
Skip to content

fix cross-app modifier key states#5209

Open
btarb24 wants to merge 2 commits intoScottPlot:mainfrom
btarb24:modifierKeys
Open

fix cross-app modifier key states#5209
btarb24 wants to merge 2 commits intoScottPlot:mainfrom
btarb24:modifierKeys

Conversation

@btarb24
Copy link
Copy Markdown
Contributor

@btarb24 btarb24 commented Feb 23, 2026

The KeyboardState class only receives key information if the application has focus. This causes some issues with modifier keys.

  • if you focus your application > use ALT-Tab to switch out > click back on the chart to attempt to drag pan it = ALT is still applied even though you had already let it go, which causes a rectangle zoom instead of the desired drag pan.
  • you are in another app and you want to use a modifier key with your mouse press back onto the chart, the modifier key is ignored since the app didn't have focus when you received it.

Refactoring the keyboard system entirely may be the best option, but i didn't want to dive that deeply. Instead, i just put in a low-risk workaround specific to modifier keys.

if .net standard is ever dropped then we could remove the pinvoke and simply go with System.Windows.Input's Keyboard.Modifiers.HasFlag

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in cross-platform code, so we can't pull in a Windows library.

One option would be to requery key state when the control is focused and then have the control update the key state itself. That would presumably be fine everywhere. Another option would be to have this class take an interface which can call whichever platform-specific code is necessary to check key state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I reverted the changes to seek a new solution.

I saw WinUIPlotExtensions has a UpdateKeyStateFromModifiers method that looks at the framework's keyboard state system and updates the KeyState to match which modifiers the framework says are pressed. I used the same pattern for WpfUIPlotExtensions (and used Claude for the other frameworks I'm less familiar with).

The result should be that this now works with all of the supported frameworks and is crossPlatform on the frameworks that support it. Tested only with WPF on Windows. Both of my bullet points in initial comment pass.

@btarb24 btarb24 requested a review from bclehmann March 31, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants