2019-06-04 23:07:04 +00:00
|
|
|
|
/*
|
2019-06-07 20:10:52 +00:00
|
|
|
|
* Copyright 2019 faddenSoft
|
2019-06-04 23:07:04 +00:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
|
|
|
namespace CommonWPF {
|
2019-06-07 20:10:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handle a multi-key input sequence for WPF windows.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Also posted as https://stackoverflow.com/a/56452142/294248
|
|
|
|
|
///
|
|
|
|
|
/// Example:
|
|
|
|
|
/// {RoutedUICommand}.InputGestures.Add(
|
|
|
|
|
/// new MultiKeyInputGesture(new KeyGesture[] {
|
|
|
|
|
/// new KeyGesture(Key.H, ModifierKeys.Control, "Ctrl+H"),
|
|
|
|
|
/// new KeyGesture(Key.C, ModifierKeys.Control, "Ctrl+C")
|
|
|
|
|
/// }) );
|
|
|
|
|
/// </remarks>
|
2019-06-04 23:07:04 +00:00
|
|
|
|
public class MultiKeyInputGesture : InputGesture {
|
2019-06-07 20:10:52 +00:00
|
|
|
|
private const int MAX_PAUSE_MILLIS = 2000;
|
2019-06-04 23:07:04 +00:00
|
|
|
|
|
|
|
|
|
private InputGestureCollection mGestures = new InputGestureCollection();
|
|
|
|
|
|
|
|
|
|
private DateTime mLastWhen = DateTime.Now;
|
|
|
|
|
private int mCheckIdx;
|
|
|
|
|
private string mIdStr;
|
|
|
|
|
|
2019-06-07 20:10:52 +00:00
|
|
|
|
// On a successful match, the handler "eats" the final keypress. If you have multiple
|
|
|
|
|
// handlers, the ones that are called later won't see the non-matching key and will
|
|
|
|
|
// still be waiting. This can be a problem if the user types multiple multi-key
|
|
|
|
|
// sequences in rapid succession (or even not-so-rapid if you disable the timeout). To
|
|
|
|
|
// deal with this, all instances subscribe to this event, which fires when a match is
|
|
|
|
|
// found.
|
|
|
|
|
private delegate void GotMatchHandler(object sender, EventArgs e);
|
|
|
|
|
private static event GotMatchHandler sGotMatch;
|
2019-06-04 23:07:04 +00:00
|
|
|
|
|
2019-06-07 20:10:52 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="keys">Sequence of keys to watch for.</param>
|
2019-06-04 23:07:04 +00:00
|
|
|
|
public MultiKeyInputGesture(KeyGesture[] keys) {
|
2019-06-07 20:10:52 +00:00
|
|
|
|
Debug.Assert(keys.Length > 0); // arguably also bad input if == 1
|
2019-06-04 23:07:04 +00:00
|
|
|
|
|
|
|
|
|
StringBuilder idSb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
// Grab a copy of the array contents.
|
|
|
|
|
foreach (KeyGesture kg in keys) {
|
|
|
|
|
mGestures.Add(kg);
|
|
|
|
|
idSb.Append(kg.DisplayString[kg.DisplayString.Length - 1]);
|
|
|
|
|
}
|
|
|
|
|
mIdStr = idSb.ToString();
|
2019-06-07 20:10:52 +00:00
|
|
|
|
|
|
|
|
|
sGotMatch += delegate(object sender, EventArgs e) {
|
|
|
|
|
mCheckIdx = 0;
|
|
|
|
|
};
|
2019-06-04 23:07:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 20:10:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// InputGesture interface. Tests an input event to see if it's part of a sequence.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="targetElement">Not used.</param>
|
|
|
|
|
/// <param name="inputEventArgs">Input event. Ignored if not a key event.</param>
|
|
|
|
|
/// <returns>True if the key matches and we're at the end of the sequence.</returns>
|
2019-06-04 23:07:04 +00:00
|
|
|
|
public override bool Matches(object targetElement, InputEventArgs inputEventArgs) {
|
|
|
|
|
if (!(inputEventArgs is KeyEventArgs)) {
|
|
|
|
|
// does this actually happen?
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DateTime now = DateTime.Now;
|
|
|
|
|
if ((now - mLastWhen).TotalMilliseconds > MAX_PAUSE_MILLIS) {
|
|
|
|
|
//Debug.WriteLine("MKIG " + mIdStr + ": too long since last key (" +
|
|
|
|
|
// (now - mLastWhen).TotalMilliseconds + " ms");
|
|
|
|
|
mCheckIdx = 0;
|
|
|
|
|
}
|
|
|
|
|
mLastWhen = now;
|
|
|
|
|
|
|
|
|
|
if (((KeyEventArgs)inputEventArgs).IsRepeat) {
|
|
|
|
|
// ignore key-repeat noise (especially from modifiers)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!mGestures[mCheckIdx].Matches(null, inputEventArgs)) {
|
|
|
|
|
if (mCheckIdx > 0) {
|
|
|
|
|
//Debug.WriteLine("MKIG " + mIdStr + ": no match, resetting");
|
|
|
|
|
mCheckIdx = 0;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Debug.WriteLine("MKIG " + mIdStr + ": matched gesture #" + mCheckIdx);
|
|
|
|
|
mCheckIdx++;
|
|
|
|
|
if (mCheckIdx == mGestures.Count) {
|
|
|
|
|
//Debug.WriteLine("MKIG " + mIdStr + ": match");
|
|
|
|
|
mCheckIdx = 0;
|
|
|
|
|
inputEventArgs.Handled = true;
|
2019-06-07 20:10:52 +00:00
|
|
|
|
|
|
|
|
|
// signal other instances
|
|
|
|
|
sGotMatch(this, null);
|
2019-06-04 23:07:04 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|