refine input event system and binding logic to allow more natural use of modifier keys as hotkeys

This commit is contained in:
zeromus 2012-11-09 20:03:59 +00:00
parent e13e3257c1
commit 9053ac5b67
5 changed files with 90 additions and 41 deletions

View File

@ -130,6 +130,7 @@ namespace BizHawk.MultiClient
WorkingDictionary<string, object> ModifierState = new WorkingDictionary<string, object>();
WorkingDictionary<string, bool> LastState = new WorkingDictionary<string, bool>();
WorkingDictionary<string, bool> UnpressState = new WorkingDictionary<string, bool>();
HashSet<string> IgnoreKeys = new HashSet<string>(new[] { "LeftShift", "RightShift", "LeftControl", "RightControl", "LeftAlt", "RightAlt" });
void HandleButton(string button, bool newState)
@ -138,10 +139,27 @@ namespace BizHawk.MultiClient
if (LastState[button] && newState) return;
if (!LastState[button] && !newState) return;
if (UnpressState.ContainsKey(button))
{
if (newState) return;
Console.WriteLine("Removing Unpress {0} with newState {1}", button, newState);
UnpressState.Remove(button);
LastState[button] = false;
return;
}
//dont generate events for things like Ctrl+LeftControl
ModifierKey mods = _Modifiers;
if (button == "LeftShift") mods &= ~ModifierKey.Shift;
if (button == "RightShift") mods &= ~ModifierKey.Shift;
if (button == "LeftControl") mods &= ~ModifierKey.Control;
if (button == "RightControl") mods &= ~ModifierKey.Control;
if (button == "LeftAlt") mods &= ~ModifierKey.Alt;
if (button == "RightAlt") mods &= ~ModifierKey.Alt;
var ie = new InputEvent();
ie.EventType = newState ? InputEventType.Press : InputEventType.Release;
ie.LogicalButton = new LogicalButton(button, _Modifiers);
_NewEvents.Add(ie);
ie.LogicalButton = new LogicalButton(button, mods);
LastState[button] = newState;
//track the pressed events with modifiers that we send so that we can send corresponding unpresses with modifiers
@ -160,14 +178,16 @@ namespace BizHawk.MultiClient
if (ModifierState[button] != null)
{
LogicalButton alreadyReleased = ie.LogicalButton;
ie = new InputEvent();
ie.LogicalButton = (LogicalButton)ModifierState[button];
ie.EventType = InputEventType.Release;
if(ie.LogicalButton != alreadyReleased)
_NewEvents.Add(ie);
var ieModified = new InputEvent();
ieModified.LogicalButton = (LogicalButton)ModifierState[button];
ieModified.EventType = InputEventType.Release;
if (ieModified.LogicalButton != alreadyReleased)
_NewEvents.Add(ieModified);
}
ModifierState[button] = null;
}
_NewEvents.Add(ie);
}
ModifierKey _Modifiers;
@ -264,14 +284,43 @@ namespace BizHawk.MultiClient
}
//returns the next Press event, if available. should be useful
public string GetNextPressedButtonOrNull()
public string GetNextBindEvent()
{
InputEvent ie = DequeueEvent();
if (ie == null) return null;
if (ie.EventType == InputEventType.Release) return null;
if (InputEvents.Count == 0) return null;
//we only listen to releases for input binding, because we need to distinguish releases of pure modifierkeys from modified keys
//if you just pressed ctrl, wanting to bind ctrl, we'd see: pressed:ctrl, unpressed:ctrl
//if you just pressed ctrl+c, wanting to bind ctrl+c, we'd see: pressed:ctrl, pressed:ctrl+c, unpressed:ctrl+c, unpressed:ctrl
//so its the first unpress we need to listen for
while (InputEvents.Count != 0)
{
var ie = DequeueEvent();
//as a special perk, we'll accept escape immediately
if (ie.EventType == InputEventType.Press && ie.LogicalButton.Button == "Escape")
goto ACCEPT;
if (ie.EventType == InputEventType.Press) continue;
ACCEPT:
Console.WriteLine("Bind Event: {0} ", ie);
foreach (var kvp in LastState)
if (kvp.Value)
{
Console.WriteLine("Unpressing " + kvp.Key);
UnpressState[kvp.Key] = true;
}
InputEvents.Clear();
return ie.LogicalButton.ToString();
}
return null;
}
//controls whether modifier keys will be ignored as key press events
//this should be used by hotkey binders, but we may want modifier key events
//to get triggered in the main form

View File

@ -97,14 +97,14 @@ namespace BizHawk.MultiClient
protected override void OnShown(EventArgs e)
{
Input.Instance.EnableIgnoreModifiers = true;
//Input.Instance.EnableIgnoreModifiers = true;
base.OnShown(e);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Input.Instance.EnableIgnoreModifiers = false;
//Input.Instance.EnableIgnoreModifiers = false;
}
private void SetAutoTab(bool setting)

View File

@ -122,14 +122,14 @@ namespace BizHawk.MultiClient
protected override void OnShown(EventArgs e)
{
Input.Instance.EnableIgnoreModifiers = true;
//Input.Instance.EnableIgnoreModifiers = true;
base.OnShown(e);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Input.Instance.EnableIgnoreModifiers = false;
//Input.Instance.EnableIgnoreModifiers = false;
}
private void IDB_SAVE_Click(object sender, EventArgs e)

View File

@ -68,14 +68,14 @@ namespace BizHawk.MultiClient
protected override void OnShown(EventArgs e)
{
Input.Instance.EnableIgnoreModifiers = true;
//Input.Instance.EnableIgnoreModifiers = true;
base.OnShown(e);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Input.Instance.EnableIgnoreModifiers = false;
//Input.Instance.EnableIgnoreModifiers = false;
}
private void Do(string platform)

View File

@ -90,7 +90,7 @@ namespace BizHawk.MultiClient
//Input.Update();
//zero: ??? what is this all about ???
wasPressed = Input.Instance.GetNextPressedButtonOrNull();
wasPressed = Input.Instance.GetNextBindEvent();
}
protected override void OnLeave(EventArgs e)
@ -115,7 +115,7 @@ namespace BizHawk.MultiClient
private void ReadKeys()
{
Input.Instance.Update();
string TempBindingStr = Input.Instance.GetNextPressedButtonOrNull();
string TempBindingStr = Input.Instance.GetNextBindEvent();
if (wasPressed != "" && TempBindingStr == wasPressed)
{
return;