• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6599225437

21 Oct 2023 07:19PM UTC coverage: 44.768% (-7.8%) from 52.519%
6599225437

Pull #50

github

web-flow
Merge 2fe0423ed into 14b7ce9a7
Pull Request #50: Add XInput in preparation for gamepad triggers + add xmldoc

758 of 2325 branches covered (0.0%)

Branch coverage included in aggregate %.

2834 of 2834 new or added lines in 98 files covered. (100.0%)

3875 of 8024 relevant lines covered (48.29%)

13037.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Key2Joy.Gui/Mapping/Triggers/Keyboard/KeyboardTriggerControl.cs
1
using System;
2
using System.Windows.Forms;
3
using Key2Joy.Contracts.Mapping;
4
using Key2Joy.Contracts.Mapping.Triggers;
5
using Key2Joy.LowLevelInput;
6
using Key2Joy.Mapping.Triggers;
7
using Key2Joy.Mapping.Triggers.Keyboard;
8

9
namespace Key2Joy.Gui.Mapping;
10

11
[MappingControl(
12
    ForType = typeof(KeyboardTrigger),
13
    ImageResourceName = "keyboard"
14
)]
15
public partial class KeyboardTriggerControl : UserControl, ITriggerOptionsControl
16
{
17
    private const string TEXT_CHANGE = "(press any key to select it as the trigger)";
18
    private const string TEXT_CHANGE_INSTRUCTION = "(click here, then press any key to set it as the trigger)";
19
    private readonly VirtualKeyConverter virtualKeyConverter = new();
×
20

21
    public event EventHandler OptionsChanged;
22

23
    private Keys keys;
24
    private bool isTrapping;
25

26
    public KeyboardTriggerControl()
×
27
    {
28
        this.InitializeComponent();
×
29

30
        // This captures global keyboard input and blocks default behaviour by setting e.Handled
31
        GlobalInputHook globalKeyboardHook = new();
×
32
        globalKeyboardHook.KeyboardInputEvent += this.OnKeyInputEvent;
×
33

34
        // Relieve input capturing by this mapping form
35
        Disposed += (s, e) =>
×
36
        {
×
37
            if (globalKeyboardHook == null)
×
38
            {
×
39
                return;
×
40
            }
×
41

×
42
            globalKeyboardHook.KeyboardInputEvent -= this.OnKeyInputEvent;
×
43
            globalKeyboardHook.Dispose();
×
44
            globalKeyboardHook = null;
×
45
        };
×
46
        ControlRemoved += (s, e) => this.Dispose();
×
47

48
        this.cmbPressState.DataSource = PressStates.ALL;
×
49
        this.cmbPressState.SelectedIndex = 0;
×
50

51
        // Removed because this is annoying when you want to just edit code
52
        //StartTrapping();
53
    }
×
54

55
    private void OnKeyInputEvent(object sender, GlobalKeyboardHookEventArgs e)
56
    {
57
        if (!this.isTrapping)
×
58
        {
59
            return;
×
60
        }
61

62
        this.keys = this.virtualKeyConverter.KeysFromVirtual(e.KeyboardData.VirtualCode);
×
63
        this.UpdateKeys();
×
64
        this.StopTrapping();
×
65
    }
×
66

67
    public void Select(AbstractTrigger trigger)
68
    {
69
        var thisTrigger = (KeyboardTrigger)trigger;
×
70

71
        this.keys = thisTrigger.Keys;
×
72
        this.cmbPressState.SelectedItem = thisTrigger.PressState;
×
73
        this.UpdateKeys();
×
74
    }
×
75

76
    public void Setup(AbstractTrigger trigger)
77
    {
78
        var thisTrigger = (KeyboardTrigger)trigger;
×
79

80
        thisTrigger.Keys = this.keys;
×
81
        thisTrigger.PressState = (PressState)this.cmbPressState.SelectedItem;
×
82
    }
×
83

84
    public bool CanMappingSave(AbstractTrigger trigger)
85
    {
86
        var thisTrigger = (KeyboardTrigger)trigger;
×
87

88
        if (thisTrigger.Keys != Keys.None)
×
89
        {
90
            return true;
×
91
        }
92

93
        MessageBox.Show(
×
94
            $"The trigger is not set to any key.",
×
95
            "Cannot save!",
×
96
            MessageBoxButtons.OK,
×
97
            MessageBoxIcon.Error
×
98
        );
×
99

100
        return false;
×
101
    }
102

103
    private void StartTrapping()
104
    {
105
        this.txtKeyBind.Text = TEXT_CHANGE;
×
106
        this.txtKeyBind.Focus();
×
107
        this.isTrapping = true;
×
108
    }
×
109

110
    private void StopTrapping() => this.isTrapping = false;
×
111

112
    private void UpdateKeys()
113
    {
114
        this.txtKeyBind.Text = $"{this.keys} {TEXT_CHANGE_INSTRUCTION}";
×
115
        OptionsChanged?.Invoke(this, EventArgs.Empty);
×
116
    }
×
117

118
    private void CmbPressedState_SelectedIndexChanged(object sender, EventArgs e) => OptionsChanged?.Invoke(this, EventArgs.Empty);
×
119

120
    private void TxtKeyBind_MouseUp(object sender, MouseEventArgs e) => this.StartTrapping();
×
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc