Nob*_*fer 1 c# wpf xaml keyboard-shortcuts keypress
我正在使用由ChromiumWebBrowser提供的向我的C#应用程序的用户显示网站。CefSharp库。
我正在尝试添加功能,以允许用户使用键盘快捷键(即CTRL +/)“放大/缩小” CTRL -。
我设法KeyboardListener使用“低级全局键盘挂钩/接收器”将其添加到嵌入式浏览器中,该网址位于:http : //www.dylansweb.com/2014/10/low-level-global-keyboard-hook-sink-in -c-net /
目前,当浏览器处于“焦点对准”状态并且用户按下键盘上的“ +”时,我的应用程序将在浏览器中“放大”。我使用以下方法完成此操作:
private void _listener_OnKeyPressed(object sender, KeyPressedArgs e)
{
if(e.KeyPressed == Key.Add)
{
zoomInExecuted();
}
}
Run Code Online (Sandbox Code Playgroud)
我真正想要的只是在用户按住“ CTRL”键然后按“ +”时才允许放大。
我在C#中编写了以下方法(此方法称为:
private void _listener_OnKeyPressed(object sender, KeyPressedArgs e)
{
Debug.WriteLine("e: " + e.KeyPressed.ToString());
if(e.KeyPressed == Key.LeftCtrl)
{
leftCtrlDown = true;
Debug.WriteLine("LeftCtrl pressed, leftCtrlDown should be true: ", leftCtrlDown.ToString());
}
else
{
leftCtrlDown = false;
}
if(e.KeyPressed == Key.RightCtrl)
{
rightCtrlDown = true;
Debug.WriteLine("RightCtrl pressed, rightCtrlDown should be true: ", rightCtrlDown.ToString());
}
else
{
rightCtrlDown = false;
}
if((leftCtrlDown == true)) //&& (e.KeyPressed == Key.Add))
{
if (e.KeyPressed == Key.Add)
{
Debug.WriteLine("Ctrl & + pressed, 'zoomInExecuted()' should be called ");
zoomInExecuted();
}
}else if((rightCtrlDown == true)) //&& (e.KeyPressed == Key.Add))
{
if (e.KeyPressed == Key.Add)
{
Debug.WriteLine("rightCtrl & + pressed, 'zoomInExecuted()' should be called ");
zoomInExecuted();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用XAML中显示浏览器的<KeyBinding>标签来调用此方法<Grid>:
<KeyBinding Modifiers="Ctrl" Key="LeftCtrl" Command="{Binding _listener_OnKeyPressed}"></KeyBinding>
Run Code Online (Sandbox Code Playgroud)
但是我遇到的问题是:尽管应用程序检测到何时按下了“ CTRL”键(调试已写入控制台),但似乎无法检测到第二个键的按下( “ +”键)。
我尝试添加第二个侦听器,仅当leftCtrlDown或rightCtrlDown布尔值为true时(即,用户按下CTRL键时),才在第一个侦听器中调用它,但是应用程序似乎仍然无法检测到按第二个键...
在已经确认当前正在按下一个键的同时,如何使我的应用“监听”另一个键?
编辑
我尝试做答案中建议的操作,现在在我的XAML中使用:
<Window x:Class="..."
....
xmlns:local="clr-namespace:Agent"
... >
<Window.Resources>
...
</Window.Resources>
<Grid Name="grid">
...
<Grid x:Name="grdBrowserHost" MinHeight="900" Height="Auto" MinWidth="1205" Width="Auto" Margin="5,0,0,0" DockPanel.Dock="Bottom" Grid.ColumnSpan="1" >
<Grid.InputBindings>
<KeyBinding Modifiers="Ctrl" Key="Add" Command="{Binding _listener_OnKeyPressed}"></KeyBinding>
</Grid.InputBindings>
...
<cefSharp:ChromiumWebBrowser Name="browser" ...>
<KeyBinding Modifiers="Ctrl" Key="Add">
<KeyBinding.Command>
<local:Zoom Executed="zoomInExecuted" />
</KeyBinding.Command>
</KeyBinding>
</cefSharp:ChromiumWebBrowser.InputBindings>
</Grid>
</Grid>
...
</Window>
Run Code Online (Sandbox Code Playgroud)
Zoom.cs我添加的类如下:
namespace Agent
{
class Zoom : ICommand
{
public event EventHandler<object> Executed;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
if (Executed != null)
Executed(this, parameter);
}
public event EventHandler CanExecuteChanged;
}
}
Run Code Online (Sandbox Code Playgroud)
但是由于某种原因,我在XAML上遇到了一个编译错误:
<local:Zoom Executed="zoomInExecuted" />
Run Code Online (Sandbox Code Playgroud)
其中说:
名称“ Zoom”在名称空间“ clr-namespace:Agent”中不存在。
即使很明显。
该行不起作用:
<KeyBinding Modifiers="Ctrl" Key="LeftCtrl" Command="{Binding _listener_OnKeyPressed}"/>
Run Code Online (Sandbox Code Playgroud)
KeyBinding.Command期望对象实现ICommand,您将其绑定到方法。
该ICommand接口的基本实现如下所示:
class SimpleCommand : ICommand
{
public event EventHandler<object> Executed;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
if (Executed != null)
Executed(this, parameter);
}
public event EventHandler CanExecuteChanged;
}
Run Code Online (Sandbox Code Playgroud)
您可以这样使用:
<Window.InputBindings>
<KeyBinding Modifiers="Control" Key="Add">
<KeyBinding.Command>
<local:SimpleCommand Executed="SimpleCommand_OnExecuted"/>
</KeyBinding.Command>
</KeyBinding>
</Window.InputBindings>
Run Code Online (Sandbox Code Playgroud)
并在后面的代码中:
private void SimpleCommand_OnExecuted(object sender, object e)
{
MessageBox.Show("SimpleCommand Executed");
}
Run Code Online (Sandbox Code Playgroud)
通常,您将使用Commanding在代码中定义Command并在XAML中使用它。当您绑定该命令的KeyBinding,Button或MenuItem(或别的东西),在CanExecute你的实现方法可用于禁用指令(因此禁用元素,它绑定到)。
| 归档时间: |
|
| 查看次数: |
5788 次 |
| 最近记录: |