1

I'd like to write an extension method:

static public bool IsKeyPressedAny(this params System.Windows.Input.Key[] keys)
{
    foreach (var key in keys)
    {
        if (System.Windows.Input.Keyboard.IsKeyDown(k))
        {
            return true;
        }
    }
    return false;
}

to be used like this

IsKeyPressedAny(Key.LeftShift, Key.RightShift);

or

IsKeyPressedAny(Key.a)

But the method's signature is invalid, adding this before params causes error CS1104: "A parameter array cannot be used with 'this' modifier on an extension method".

My current workaround is

static public bool IsKeyPressedAny(
    this System.Windows.Input.Key key
    , params System.Windows.Input.Key[] keys
)
{
    if (System.Windows.Input.Keyboard.IsKeyDown(key)) return true;

    foreach (var k in keys)
    {
        if (System.Windows.Input.Keyboard.IsKeyDown(k))
        {
            return true;
        }
    }
    return false;
}

which strikes me as a bit clunky. Is there a way to keep the benefits of using params while avoiding duplication of the argument type in the signature?

(kind of a) Solution

The comments made me realize I was missusing this. Since this is added as a method to the type it preceeds in the signature, using it before params is nonsense. I was trying to avoid typing the name of the class containing the method, this is not the way to solve that.

ooak
  • 680
  • 6
  • 8
  • 5
    I don't think an extension method is what you're looking for. Certainly the calling syntax you've listed doesn't resemble their intended use (extension methods are designed to look like "extensions" of single instances that you call like instance methods). You probably just want a helper class with a helper method. – BoltClock Sep 25 '18 at 09:47
  • 1
    There is no point in `this` if you still call the method like a regular static method. To benefit from `this`, you'd have to call it like `Key.LeftShift.IsKeyPressedAny( ... )`. Thus, rethink if you really need `this` there. – Wiktor Zychla Sep 25 '18 at 09:48
  • 1
    Does https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1104 help? What happens if you remove `this`? – mjwills Sep 25 '18 at 09:48
  • https://stackoverflow.com/questions/31852389/how-do-i-use-the-c6-using-static-feature – mjwills Sep 25 '18 at 10:09
  • @mjwills That is not quite what I was looking for. I'd like to write "namespace_name.method_name" instead of "namespace_name.helper_class_name.method_name". As far as I understand using static namespace_name.helper_class_name doesn't allow this. – ooak Sep 25 '18 at 10:18

3 Answers3

3

Consider to give the class a useful, somewhat specific name to get a nice syntax like this:

KeyPressed.Any(Key.LeftShift, Key.RightShift);

Is implemented by

public static class KeyPressed
{
    public static bool Any(params System.Windows.Input.Key[] keys)
    {
        ....
    }
}

Well, this doesn't really answer your question, but could still be a useful solution.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
2

this adds the method to the type it preceeds in the signature, using it before params makes no sense.

this is not intended to save you typing the class' name containing the method.

ooak
  • 680
  • 6
  • 8
0

What you are looking for is called "using static", it imports things just like a regular usingdirective for namespaces:

using static YourNameSpace.KeyHelpers;

will allow you to call YourNameSpace.KeyHelpers.IsKeyPressedAny by just it's name, without the class name:

IsKeyPressedAny(Key.LeftShift, Key.RightShift);

Example:

public static class KeyHelpers
{
    public static bool IsKeyPressedAny(params System.Windows.Input.Key[] keys)
    {
        foreach (var key in keys)
        {
            if (System.Windows.Input.Keyboard.IsKeyDown(k))
            {
                return true;
            }
        }
        return false;
    }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I prefer `Any` method that Stefan mentioned, instead of `IsKeyPressedAny`. – Avestura Sep 25 '18 at 11:04
  • To be honest, I prefer the naming variant, too. But this is the language feature that does *exactly* what the OP asked for, so I think it should be mentioned. – nvoigt Sep 25 '18 at 11:06
  • Thank you, this example made me aware of how using static works :-) – DhyMik Oct 08 '21 at 14:09