我的小项目进展顺利,但是我绊倒了一些可能很愚蠢的事情...
以某种方式,当我打开应用程序时,什么都没有得到焦点,我必须使用“ tab”键将焦点移至命令栏并能够使用键盘快捷键。
然后...
当我使用Scrollviewer来移动图像或缩放时,除非使用“制表符”将其移动到命令栏,否则无法再次使用键盘快捷键。
我尝试过
cmdbar.Focus(FocusState.Programmatic);
在应用程序的任何地方,我认为它可能很有用,但无济于事。我也尝试过使用键盘加速器,但这无济于事。有提示吗?
这是我的XAML代码:
<Page.Resources>
<DataTemplate x:Key="myResourceTemplate">
<TextBlock Text="{Binding}" MaxHeight="10" FontSize="8" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold" LineHeight="9" Height="Auto" />
</DataTemplate>
</Page.Resources>
<Page.BottomAppBar>
<CommandBar x:Name="cmdbar" ClosedDisplayMode="Compact" HorizontalAlignment="Left" HorizontalContentAlignment="Left" VerticalAlignment="Center" KeyUp="kb_openkey" Opacity="1" Visibility="Visible" Background="#260000FF">
<CommandBar.Content>
<Grid/>
</CommandBar.Content>
<AppBarButton Icon="ZoomIn" Label="AppBarButton" Tapped="Zoomin_Click"/>
<AppBarButton Icon="ZoomOut" Label="AppBarButton" Tapped="Zoomout_Click"/>
<AppBarToggleButton x:Name="randomstatus" Icon="Shuffle" Label="Random" Tapped="Togglerandom"/>
<... a bunch of other buttons >
</CommandBar>
</Page.BottomAppBar>
<Grid x:Name="imggrid" Background="Black" BorderBrush="Black" KeyUp="kb_openkey">
<ScrollViewer x:Name="imageView_scroller" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" ZoomMode="Enabled" RequestedTheme="Dark" KeyUp="kb_openkey">
<Image x:Name = "ctlImage" Grid.Column ="0" VerticalAlignment = "Stretch" HorizontalAlignment = "Stretch" Stretch = "Uniform"
PointerWheelChanged="ctlImage_PointerWheelChanged"
ManipulationMode = "TranslateX, TranslateY, Scale"
ManipulationStarted = "ctlImage_ManipulationStarted"
ManipulationDelta = "ctlImage_ManipulationDelta"
ManipulationCompleted = "ctlImage_ManipulationCompleted"
KeyUp="kb_openkey"
>
<Image.RenderTransform>
<CompositeTransform x:Name="image_Transform" ></CompositeTransform >
</Image.RenderTransform >
</Image>
</ScrollViewer>
</Grid>
这是我处理键盘输入的方式:
void kb_openkey(object sender, KeyRoutedEventArgs e)
{
if ((int)e.Key >= 1 && (int)e.Key <= 255)
{
switch ((int)e.Key)
{
case 70: //A
....dothis....;
break;
case 65: //A
.... dothat....;
break;
}
}
}
答案 0 :(得分:0)
您无需设置焦点即可将KeyboardAccelrators用作快捷方式。因此,除非它们具有与设置焦点无关的其他任务,否则您不需要图像或命令栏上的 keyup或keydown 事件。
您应该在命令栏中使用 AccessKey 和任何选项修饰符(例如 Ctrl 或 Shift
)使用KeyBoardAccelratorsAppBarButton
上AccessKey的示例<AppBarButton
Icon="Copy"
Label="Copy"
ToolTipService.ToolTip="Copy (Ctrl+C)"
Click="OnCopy"
AccessKey="C">
<AppBarButton.KeyboardAccelerators>
<KeyboardAccelerator
Modifiers="Control"
Key="C" />
</AppBarButton.KeyboardAccelerators>
</AppBarButton>
您可以在上面提供的文档链接中找到更多详细信息。
更新:
当您点击另一个UI元素时,上一个元素的焦点会自动删除,您的图像以及命令栏上都不需要 KeyUp 事件,您只需使用全局{{3 }}可以帮助您完成您想完成的所有与关键相关的命令
答案 1 :(得分:0)
@touseefbsb,非常有用!!!谢谢!不管有什么被关注和被点击,都可以使用此键。
所以我的代码仅供参考
在XAML的页面部分中,添加:
Loaded="initkbd"
Unloaded="unloadkbd"
然后在C#部分中添加:
//Add the key handler method to the KeyDown handlers
private void initkbd(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.KeyDown += kb_openkey;
cmdbar.Content = "Added to keys";
}
//Remove the keyhandler method from the list
private void unloadkbd(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.KeyDown -=kb_openkey;
}
然后,密钥处理程序如下所示:
private void kb_openkey(CoreWindow sender, KeyEventArgs e)
{
//Mark the event as handled
e.Handled = true;
int keypressed = (int) e.VirtualKey;
//Than handle the key, based on its keycode
if ((int)keypressed >= 1 && (int)keypressed <= 255)
{
switch (keypressed)
{
case 70: //F
//do something when F is presed
break;
case 76: //L dialog to show items
//Do something when L is pressed
break;
}
}
}