4

I currently have this code:

        DataGrid.SelectionChanged += new SelectionChangedEventHandler(DataGrid_SelectionChanged);

        private void DataGrid_SelectionChanged(object sender, EventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            {
                //do stuff
            }
        }

The code checks for the Ctrl button when the selection in a data grid changes which works fine for detecting the Ctrl key for normal physical keys but when I use the on screen keyboard and press down the Ctrl key it doesn't register in my program. The root of this code is that you cannot Ctrl+Click the rows in a Datagrid in WPF when you use the Ctrl key of the on screen keyboard.

Edit: It seems to satisfy the if condition if, on the on screen keyboard, I press ctrl and then "A" (which selects all). None of the other shortcuts (ctrl+c, ctrl + v trigger this response)

  • @HansPassant I added some more code and comments explaining how it's called and why I ran into this problem –  Jul 27 '15 at 20:37
  • It works just fine when I try it. Pretty clumsy UI of course, not getting the Ctrl key released when you click is very ugly, maybe that's part of the problem. – Hans Passant Jul 27 '15 at 20:56
  • @HansPassant really? hmmph. When I set a break point inside the if statement it only gets triggered when I use the CTRL button on my keyboard and not on the on screen keyboard. –  Jul 27 '15 at 21:06
  • Well, what are you *really* trying to do? It looks like a hack to detect that multiple rows of the grid are getting selected. That's not the right way to do it surely is better that we don't have to guess. – Hans Passant Jul 27 '15 at 21:13
  • @HansPassant yeah that's exactly what is happening. I expected ctrl on the touch screen to work the same and it wasn't so I was gonna hack around. I don't know how else to really get around the problem though. –  Jul 27 '15 at 21:26
  • I'm afraid it's working for me too, at least using the Windows 8.1 default on-screen keyboard. I'm not sure I see a problem with the question - Ctrl is the standard modifier for making non-contiguous selections. I'm genuinely curious as to how else @HansPassant would normally implement it. – goobering Jul 27 '15 at 21:28
  • @HansPassant Weird devlopment. If, in the on screen keyboard, I press ctrl and then "A" (which selects all) it accepts that and I hit the break point. None of the other shortcuts (ctrl+c, ctrl + v trigger this response –  Jul 27 '15 at 21:34
  • Well, that is supposed to work as well. You'll never get me to sign the purchase order though, I'll hold down the shift key and press the up/down arrow keys to select multiple rows. No control key, no mouse, fail whale. This question is still XY. – Hans Passant Jul 27 '15 at 21:41
  • See my previous comment re: *non-contiguous* selections. If you want to select rows 1, 4, 9 and 15 simultaneously you use the ctrl key. If you want to select 1 through 5 you use shift. – goobering Jul 27 '15 at 22:01

1 Answers1

1

I ended up coding around the problem and added a check box to every row in the datagrid. Although this no longer allows for the use of ctrl+click and shift+click the selection mechanics works just as well as it does on a touch screen as it does on a physical keyboard. I hope eventually the on screen keyboard will be more full featured and have less "bugs".