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

luttje / Key2Joy / 6602636543

22 Oct 2023 08:16AM UTC coverage: 44.104% (-8.4%) from 52.519%
6602636543

Pull #50

github

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

764 of 2383 branches covered (0.0%)

Branch coverage included in aggregate %.

3060 of 3060 new or added lines in 106 files covered. (100.0%)

3896 of 8183 relevant lines covered (47.61%)

15812.68 hits per line

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

3.45
/Core/Key2Joy.Core/Mapping/Actions/Input/KeyboardAction.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Threading.Tasks;
4
using Key2Joy.Contracts.Mapping;
5
using Key2Joy.Contracts.Mapping.Actions;
6
using Key2Joy.Contracts.Mapping.Triggers;
7
using Key2Joy.LowLevelInput;
8

9
namespace Key2Joy.Mapping.Actions.Input;
10

11
[Action(
12
    Description = "Keyboard Simulation",
13
    NameFormat = "{1} {0} on Keyboard",
14
    GroupName = "Keyboard Simulation",
15
    GroupImage = "keyboard"
16
)]
17
public class KeyboardAction : CoreAction, IPressState, IProvideReverseAspect
18
{
19
    public KeyboardKey Key { get; set; }
×
20
    public PressState PressState { get; set; }
×
21

22
    public KeyboardAction(string name)
23
        : base(name)
7✔
24
    {
25
    }
7✔
26

27
    /// <inheritdoc/>
28
    public void MakeReverse(AbstractMappingAspect aspect)
29
        => CommonReverseAspect.MakeReversePressState(this, aspect);
×
30

31
    public static KeyboardKey[] GetAllKeys()
32
    {
33
        var allEnums = Enum.GetValues(typeof(KeyboardKey));
×
34
        List<KeyboardKey> keys = new();
×
35

36
        // Skip the enumerations that are zero
37
        foreach (var keyEnum in allEnums)
×
38
        {
39
            if ((short)keyEnum == 0)
×
40
            {
41
                continue;
42
            }
43

44
            keys.Add((KeyboardKey)keyEnum);
×
45
        }
46

47
        return keys.ToArray();
×
48
    }
49

50
    public static List<MappedOption> GetAllButtonActions(PressState pressState)
51
    {
52
        var actionFactory = ActionsRepository.GetAction(typeof(KeyboardAction));
×
53

54
        List<MappedOption> actions = new();
×
55
        foreach (var key in GetAllKeys())
×
56
        {
57
            var action = (KeyboardAction)MakeAction(actionFactory);
×
58
            action.Key = key;
×
59
            action.PressState = pressState;
×
60

61
            actions.Add(new MappedOption
×
62
            {
×
63
                Action = action
×
64
            });
×
65
        }
66
        return actions;
×
67
    }
68

69
    /// <markdown-doc>
70
    /// <parent-name>Input</parent-name>
71
    /// <path>Api/Input</path>
72
    /// </markdown-doc>
73
    /// <summary>
74
    /// Simulate pressing or releasing (or both) keyboard keys.
75
    /// </summary>
76
    /// <param name="key">Key to simulate</param>
77
    /// <param name="pressState">Action to simulate</param>
78
    /// <name>Keyboard.Simulate</name>
79
    [ExposesScriptingMethod("Keyboard.Simulate")]
80
    public async void ExecuteForScript(KeyboardKey key, PressState pressState)
81
    {
82
        this.Key = key;
×
83
        this.PressState = pressState;
×
84

85
        if (this.PressState == PressState.Press)
×
86
        {
87
            SimulatedKeyboard.PressKey(this.Key);
×
88
        }
89

90
        if (this.PressState == PressState.Release)
×
91
        {
92
            SimulatedKeyboard.ReleaseKey(this.Key);
×
93
        }
94
    }
×
95

96
    public override async Task Execute(AbstractInputBag inputBag = null)
97
    {
98
        if (this.PressState == PressState.Press)
×
99
        {
100
            SimulatedKeyboard.PressKey(this.Key);
×
101
        }
102
        else if (this.PressState == PressState.Release)
×
103
        {
104
            SimulatedKeyboard.ReleaseKey(this.Key);
×
105
        }
106
    }
×
107

108
    public override string GetNameDisplay() => this.Name.Replace("{0}", Enum.GetName(typeof(KeyboardKey), this.Key))
×
109
            .Replace("{1}", Enum.GetName(typeof(PressState), this.PressState));
×
110

111
    public override bool Equals(object obj)
112
    {
113
        if (obj is not KeyboardAction action)
×
114
        {
115
            return false;
×
116
        }
117

118
        return action.Key == this.Key
×
119
            && action.PressState == this.PressState;
×
120
    }
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