代码之家  ›  专栏  ›  技术社区  ›  PabloG

对一系列值执行操作的C模式是什么?

c#
  •  5
  • PabloG  · 技术社区  · 14 年前

    我有这个代码:

    if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Orange) == KeyState.Lock)
        PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Orange, 0, 0);
    
    if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Blue) == KeyState.Lock)
        PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Blue, 0, 0);
    
    if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Shift) == KeyState.Lock)
        PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Shift, 0, 0);
    
    if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Control) == KeyState.Lock)
        PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Control, 0, 0);
    
    ...
    

    我想重构将键/函数定义与 行动。key.xxx和function.xxx的类型不同。

    在python中,我可以做如下的事情:

    keys = (
        ( Key.Orange, Function.Orange ),
        ( Key.Blue  , Function.Blue ),
        ( Key.Shift , Function.Shift ),
        ...
        )
    
    psi_key = PsionTeklogix.Keyboard.Keyboard
    
    for key, func in keys:
        if psi_key.GetModifierKeyState(key) == KeyState.Lock):
            psi_key.InjectKeyboardCommand(func, 0, 0)
    

    在C中“正确的方法”是什么?

    4 回复  |  直到 14 年前
        1
  •  13
  •   Jon Skeet    14 年前

    你可以做一些非常类似的事情:

    Dictionary<Key, Function> keys = new Dictionary<Key, Function>
    {
        { Key.Orange, Function.Orange },
        { Key.Blue, Function.Blue }
        ...
    };
    
    foreach (var pair in keys)
    {
        if (Keyboard.GetModifierKeyState(pair.Key) == KeyState.Locked)
        {
            Keyboard.InjectKeyboardCommand(pair.Value, 0, 0);
        }
    }
    

    如果你想:

    foreach (var pair in keys.Where(pair => 
                     Keyboard.GetModifierKeyState(pair.Key) == KeyState.Locked)
    {
        Keyboard.InjectKeyboardCommand(pair.Value, 0, 0);
    }
    

    现在使用 Dictionary 这里有点奇怪,因为我们什么都没查到。如果使用.NET 4,则可以使用元组列表:

    var keys = new List<Tuple<Key, Function>>()
    {
        Tuple.Of(Key.Orange, Function.Orange),
        Tuple.Of(Key.Blue, Function.Blue),
        ...
    };
    

    并相应地调整回路。您也可以使用匿名类型:

    var keys = new[]
    {
        new { Key = Key.Orange, Function = Function.Orange },
        new { Key = Key.Blue, Function = Function.Blue },
        ...
    };
    

    它们基本上都是表示键/函数对的方式:)

        2
  •  1
  •   Russ    14 年前

    假设键和函数是枚举,您也可以尝试:

    foreach (Key key in Enum.GetValues(typeof(Key))) 
    {
        if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(key) == KeyState.Lock)
        {
            PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand((Function)Enum.Parse(typeof(Function), key.ToString()), 0, 0);
        }
    }
    
        3
  •  1
  •   P Daddy    14 年前
    using PsionTeklogix.Keyboard; /* assuming that's a namespace, */
                                  /* otherwise, you can optionally do: */
    /* using Keyboard = PsionTeklogix.Keyboard.Keyboard; */
    
    class Foo{
        static readonly Key[] keys = {Key.Orange, Key.Blue, Key.Shift, ...};
        static readonly Function[] functions = {Function.Orange, Function.Blue, Function.Shift, ...};
    
        static void Main(){
            for(int i = 0; i < keys.Length; i++)
                if(Keyboard.GetModifierKeyState(keys[i]) == KeyState.Lock)
                    Keyboard.InjectKeyboardCommand(func, 0, 0);
        }
    }
    

    或者,因为看起来两者都是 Key Function 是枚举, 你也用同样的 功能 每个的枚举值名称 钥匙 枚举值,您可以这样做,如果稍微慢一点,则稍微漂亮一点:

    static readonly string[] values = {"Orange", "Blue", "Shift", ...};
    
    static void Main(){
        foreach(string value in values)
            if(Keyboard.GetModifierKeyState((Key)Enum.Parse(typeof(Key), value)) == KeyState.Lock);
                Keyboard.InjectKeyboardCommand((Function)Enum.Parse(typeof(Function), value), 0, 0);
    }
    

    好吧,也许不好看。无聊的。

    但是,如果 价值观 属于 钥匙 也与 功能 (即,如果 (int)Key.Orange == (int)Function.Orange 等),然后您可以执行以下操作:

    static readonly Key[] keys = {Key.Orange, Key.Blue, Key.Shift, ...};
    
    static void Main(){
        foreach(Key key in keys)
            if(Keyboard.GetModifierKeyState(key) == KeyState.Lock);
                Keyboard.InjectKeyboardCommand((Function)key, 0, 0);
    }
    

    这些都不是 直接的 相当于python代码,更像这样:

    class KeyFunction{
        readonly Key      key;
        readonly Function function;
    
        public Key      Key     {get{return key;}}
        public Function Function{get{return function;}}
    
        public KeyFunction(Key key, Function function){
            this.key      = key;
            this.function = function;
        }
    }
    
    static readonly KeyFunction[] keyFunctions = {
        new KeyFunction(Key.Orange, Function.Orange),
        new KeyFunction(Key.Blue,   Key.Blue),
        new KeyFunction(Key.Shift,  Key.Shift),
        ...
    };
    
    static void Main(){
        foreach(KeyFunction kf in keyFunctions)
            if(Keyboard.GetModifierKeyState(kf.Key) == KeyState.Lock)
                Keyboard.InjectKeyboardCommand(kf.Function, 0, 0);
    }
    

    这是最冗长的解决方案,但它是最灵活的。

        4
  •  0
  •   Adam Robinson    14 年前

    你可以使用 Dictionary 为此。

    Dictionary<Key, Function> map = new Dictionary<Key, Function>();
    
    map.Add(Key.Orange, Function.Orange);
    map.Add(Key.Blue, Function.Blue);
    map.Add(Key.Shift, Function.Shift);
    map.Add(Key.Control, Function.Control);
    
    foreach(var pair in map)
    {
        if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(map.Key) == KeyState.Lock)
        {
            PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(map.Value, 0, 0);
        }
    }