@@ -48,6 +48,8 @@ public partial class FullscreenGrab : Window
4848 private const double MaxZoomScale = 16.0 ;
4949 private const double EdgePanThresholdPercent = 0.10 ;
5050 private const double EdgePanSpeed = 8.0 ;
51+ private const string EditPostGrabActionsTag = "EditPostGrabActions" ;
52+ private const string ClosePostGrabMenuTag = "ClosePostGrabMenu" ;
5153 private readonly DispatcherTimer edgePanTimer ;
5254
5355 #endregion Fields
@@ -270,6 +272,12 @@ private void LoadDynamicPostGrabActions()
270272 // Get the PostGrabStayOpen setting
271273 bool stayOpen = DefaultSettings . PostGrabStayOpen ;
272274
275+ // Remove any existing keyboard handler to avoid duplicates
276+ contextMenu . PreviewKeyDown -= FullscreenGrab_KeyDown ;
277+
278+ // Add keyboard handling once for the entire context menu
279+ contextMenu . PreviewKeyDown += FullscreenGrab_KeyDown ;
280+
273281 int index = 1 ;
274282 foreach ( ButtonInfo action in enabledActions )
275283 {
@@ -286,9 +294,6 @@ private void LoadDynamicPostGrabActions()
286294 // Wire up click handler
287295 menuItem . Click += PostActionMenuItem_Click ;
288296
289- // Add keyboard handling
290- contextMenu . PreviewKeyDown += FullscreenGrab_KeyDown ;
291-
292297 contextMenu . Items . Add ( menuItem ) ;
293298 index ++ ;
294299 }
@@ -298,15 +303,17 @@ private void LoadDynamicPostGrabActions()
298303 // Add "Edit this list..." menu item
299304 MenuItem editPostGrabMenuItem = new ( )
300305 {
301- Header = "Edit this list..."
306+ Header = "Edit this list..." ,
307+ Tag = EditPostGrabActionsTag
302308 } ;
303309 editPostGrabMenuItem . Click += EditPostGrabActions_Click ;
304310 contextMenu . Items . Add ( editPostGrabMenuItem ) ;
305311
306312 // Add "Close this menu" menu item
307313 MenuItem hidePostGrabMenuItem = new ( )
308314 {
309- Header = "Close this menu"
315+ Header = "Close this menu" ,
316+ Tag = ClosePostGrabMenuTag
310317 } ;
311318 hidePostGrabMenuItem . Click += HidePostGrabActions_Click ;
312319 contextMenu . Items . Add ( hidePostGrabMenuItem ) ;
@@ -1010,6 +1017,36 @@ private void Window_Unloaded(object sender, RoutedEventArgs e)
10101017 if ( RegionClickCanvas . Children . Contains ( selectBorder ) )
10111018 RegionClickCanvas . Children . Remove ( selectBorder ) ;
10121019
1020+ // Clean up dynamically created post-grab action menu items
1021+ if ( NextStepDropDownButton . Flyout is ContextMenu contextMenu )
1022+ {
1023+ contextMenu . PreviewKeyDown -= FullscreenGrab_KeyDown ;
1024+
1025+ foreach ( object item in contextMenu . Items )
1026+ {
1027+ if ( item is MenuItem menuItem )
1028+ {
1029+ if ( menuItem . Tag is ButtonInfo )
1030+ {
1031+ menuItem . Click -= PostActionMenuItem_Click ;
1032+ }
1033+ else if ( menuItem . Tag is string tag )
1034+ {
1035+ if ( tag == EditPostGrabActionsTag )
1036+ {
1037+ menuItem . Click -= EditPostGrabActions_Click ;
1038+ }
1039+ else if ( tag == ClosePostGrabMenuTag )
1040+ {
1041+ menuItem . Click -= HidePostGrabActions_Click ;
1042+ }
1043+ }
1044+ }
1045+ }
1046+
1047+ contextMenu . Items . Clear ( ) ;
1048+ }
1049+
10131050 CurrentScreen = null ;
10141051 dpiScale = null ;
10151052 TextFromOCR = null ;
@@ -1100,12 +1137,11 @@ private void TableToggleButton_Click(object? sender = null, RoutedEventArgs? e =
11001137 private void PostActionMenuItem_Click ( object sender , RoutedEventArgs e )
11011138 {
11021139 // Save check state for LastUsed tracking
1103- if ( sender is MenuItem menuItem && menuItem . Tag is ButtonInfo action )
1140+ if ( sender is MenuItem menuItem
1141+ && menuItem . Tag is ButtonInfo action
1142+ && action . DefaultCheckState == DefaultCheckState . LastUsed )
11041143 {
1105- if ( action . DefaultCheckState == DefaultCheckState . LastUsed )
1106- {
1107- PostGrabActionManager . SaveCheckState ( action , menuItem . IsChecked ) ;
1108- }
1144+ PostGrabActionManager . SaveCheckState ( action , menuItem . IsChecked ) ;
11091145 }
11101146
11111147 CheckIfAnyPostActionsSelected ( ) ;
0 commit comments