Q
組合鍵事件
4
A
回答
0
<interactivity:EventTrigger EventName="KeyDown">
<mvvmlight:EventToCommand Command="{Binding Command}" PassEventArgsToCommand="True" />
</interactivity:EventTrigger>
private void Event()
{
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
if (Keyboard.IsKeyDown(Key.C) && Keyboard.IsKeyDown(Key.T))
{
//code
}
}
}
2
使用KeyDown
事件:
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) // Is Alt key pressed
{
if (Keyboard.IsKeyDown(Key.S) && Keyboard.IsKeyDown(Key.C))
{
// do something here
}
}
0
0
您可以檢測使用下面的代碼,
private void UIElement_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Alt && Keyboard.IsKeyDown(Key.S)
&& Keyboard.IsKeyDown(Key.C))
{
//if you want, you can fire event here
}
}
3
編輯:修改後的代碼。使用Gesture
和Key
屬性是不可能的。最後聲明的屬性將用作密鑰,並且Gesture
屬性中指定的密鑰將被忽略。
下面的代碼是唯一可能與2個ModifierKeys,而不是2個鍵:
<KeyBinding Gesture="Alt+Shift+C" Command="{Binding ACommand}"/>
實現與2個鍵和一個ModifierKey
下面的文章組合鍵看起來相當有用:
0
如果目的是讓用戶輸入的字符序列與控制 鍵像評論/在Visual Studio中取消註釋宏,那麼你可以做這樣的事情(TH是很粗糙的,它只是給你它是如何工作的一個想法)
增加一條按鍵它的窗上,並擁有手勢來觀看集合的自定義控件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
namespace WpfApplication4
{
[ContentProperty("Gestures")]
public class KeyGestures : Control
{
public List<IKeyGesture> Gestures
{
get { return (List<IKeyGesture>)GetValue(GesturesProperty); }
set { SetValue(GesturesProperty, value); }
}
public static readonly DependencyProperty GesturesProperty =
DependencyProperty.Register("Gestures", typeof(List<IKeyGesture>), typeof(KeyGestures), new PropertyMetadata(null));
public List<string> CurrentSequence
{
get { return (List<string>)GetValue(CurrentSequenceProperty); }
set { SetValue(CurrentSequenceProperty, value); }
}
public static readonly DependencyProperty CurrentSequenceProperty =
DependencyProperty.Register("CurrentSequence", typeof(List<string>), typeof(KeyGestures), new PropertyMetadata(null));
public KeyGestures()
{
Gestures = new List<IKeyGesture>();
CurrentSequence = new List<string>();
}
protected override void OnInitialized(EventArgs e)
{
var hostWindow = Window.GetWindow(this);
if (hostWindow != null)
{
hostWindow.PreviewKeyDown += hostWinow_PreviewKeyDown;
hostWindow.PreviewKeyUp += hostWinow_PreviewKeyUp;
}
base.OnInitialized(e);
}
bool IsAnyKeyPressed()
{
var allPossibleKeys = Enum.GetValues(typeof(Key));
bool results = false;
foreach (var currentKey in allPossibleKeys)
{
Key key = (Key)currentKey;
if (key != Key.None)
if (Keyboard.IsKeyDown((Key)currentKey)) { results = true; break; }
}
return results;
}
void hostWinow_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (!IsAnyKeyPressed())
{
CurrentSequence.Clear();
}
}
void hostWinow_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.SystemKey == Key.None)
{
if (!CurrentSequence.Contains(e.Key.ToString()))
CurrentSequence.Add(e.Key.ToString());
}
else
if (!CurrentSequence.Contains(e.SystemKey.ToString()))
CurrentSequence.Add(e.SystemKey.ToString());
foreach (var gesture in Gestures)
if (gesture.IsComplete(this.CurrentSequence))
{
if (gesture.Command != null && gesture.Command.CanExecute(gesture.CommandParameter))
gesture.Command.Execute(gesture.CommandParameter);
System.Diagnostics.Debug.WriteLine("Completed gesture " + gesture);
}
}
}
public interface IKeyGesture
{
bool IsComplete(List<string> currentSequence);
ICommand Command { get; }
object CommandParameter { get; set; }
}
public class SequenceKeyGesture : DependencyObject, IKeyGesture
{
public string Sequence { get; set; }
public char SplitChar { get; set; }
public ICommand Command { get; set; }
public object CommandParameter
{
get { return (object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(SequenceKeyGesture), new PropertyMetadata(null));
public bool IsComplete(List<string> currentSequence)
{
string[] splitSequence = Sequence.Split(SplitChar);
if (splitSequence.Length != currentSequence.Count) return false;
if (splitSequence != null && splitSequence.Length > 0)
for (int i = 0; i < splitSequence.Length; i++)
if (splitSequence[i] != currentSequence[i])
return false;
return true;
}
public SequenceKeyGesture()
{
SplitChar = '+';
}
public override string ToString()
{
return Sequence;
}
}
}
然後可以用下面的XAML
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:KeyGestures>
<local:SequenceKeyGesture Sequence="LeftAlt~S~C" SplitChar="~" Command="{Command binding here}" CommandParameter="Action1" />
<local:SequenceKeyGesture Sequence="LeftAlt+S+V" Command="{Command binding here" CommandParameter="Action2"/>
</local:KeyGestures>
</Grid>
</Window>
書中有一個的Debug.WriteLine向您展示手勢時,你要測試的情況下被觸發,而不必設置命令使用。
相關問題
- 1. 檢測組合鍵事件
- 2. Qt多鍵組合事件
- 3. 在extjs 3.4組合中禁用ENTER鍵事件組合
- 4. 下拉式組合鍵盤事件組合框mfc
- 5. 切換鍵組合後的默認鍵事件操作
- 6. Extjs 3.4獲取組合框鍵事件的鍵值
- 7. React組合事件
- 8. 自動完成組合框onkeypress事件事件吃掉回車鍵
- 9. SWT Eclipse組合事件
- 10. Kendo UI組合框事件
- 11. WPF組合框DataBound事件?
- 12. react.js中的事件組合
- 13. 組合多個onClick事件
- 14. ZK組合框ONSELECT事件
- 15. 組合框更改事件
- 16. 重寫組合框事件
- 17. Winforms組合框SelectedValueChange事件
- 18. VB6組合框事件
- 19. 組合框在每個按鍵上觸發更改事件
- 20. JFrame的組合框事件的關鍵...幫助
- 21. 在Windows窗體應用程序中捕獲組合鍵事件
- 22. Winform中Keypreview屬性和按鍵事件的組合
- 23. Flex 3:按鍵組合觸發一個事件/功能
- 24. 生成鼠標鍵盤組合事件蟒蛇
- 25. JavaFX的:處理組合鍵和鼠標事件同時
- 26. 組合外鍵
- 27. 組合主鍵
- 28. 組合鍵
- 29. 角2結合的事件到組件
- 30. 組合框控件的點擊事件
應該同時按下鍵還是應該按順序(按住Alt,按下S然後按下C)? – Andy
鍵同時 – Ranish