There was a problem with short cuts, any help / advice would be greatly appreciated! Purpose: I need to be able to handle short keys, with and without modifiers, in my application. So, for example, I need to process the key "a", as well as "CTR + a". But I want to process them only if these keys are not managed. For example, the TextBox class accepts most keys, including some commands, such as "Ctrl + C", etc., so I donβt want to intercept these events when the TextBox will process them.
I tried using commands, as well as attaching events to KeyUp on the window, but the commands intercept the keys before the TextBox can see them, KeyDown bubbles to the Window level, even if the TextBox used the key! How can I get my window to get keys that are NOT handled by any child control? Please see the code below which does not work for me. In addition, since I have many different controls, I have the βrightβ solution: I rather do not attach handlers to every instance of the control in my window.
<Window x:Class="KeyTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.CommandBindings> <CommandBinding Command="Help" CanExecute="HelpCanExecute" Executed="HelpExecuted" /> </Window.CommandBindings> <Window.InputBindings> <KeyBinding Command="Help" Key="H" /> </Window.InputBindings> <Grid> <WrapPanel> <TextBox Name="myLOG" Width="300" Height="200" Background="LightBlue" /> <TextBox Name="myINPUT" Width="300" Height="200" /> <Button Content="JUST FOR FUN" /> </WrapPanel> </Grid>
And for C #
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace KeyTest {
When the focus is in the text field, pressing "h" invokes the command, although I want "h" to go only to the text field. In addition, if inside the text field pressing any alphanumeric key fires the KeyUp event, although, as I understand it, the text field should handle = true this event!
Thanks for the help!
source share