示例#1
0
        public DisplayAllMacros(KeyMacro[,] keyMacros)
        {
            InitializeComponent();
            for (int i = 0; i < 9; i++) // Macro
            {
                TabItem root = new TabItem()
                {
                    Header = $"Macro {i+1}"
                };
                Grid       grid           = new Grid();
                StackPanel rootStackPanel = new StackPanel();
                for (int j = 0; j < EditCreateMacro.macroKeyDepth; j++) // Macro Keys
                {
                    KeyMacro macro = keyMacros[i, j];

                    StackPanel macroPanel = new StackPanel()
                    {
                        Orientation = Orientation.Horizontal, Margin = new Thickness(0, 20, 0, 20)
                    };
                    if (macro.modifier == 0xff)
                    {
                        macroPanel.Children.Add(new TextBlock()
                        {
                            Text = $"ConsumerKey: {Enum.ToObject(typeof(ConsumerKeys), (macro.keys[0] << 8 | macro.keys[1]))}", Margin = new Thickness(10, 0, 10, 0)
                        });
                    }
                    else
                    {
                        macroPanel.Children.Add(new CheckBox()
                        {
                            IsEnabled = false, IsChecked = (macro.modifier & (1 << 0)) != 0, Content = "CTLR"
                        });
                        macroPanel.Children.Add(new CheckBox()
                        {
                            IsEnabled = false, IsChecked = (macro.modifier & (1 << 1)) != 0, Content = "SHIFT"
                        });
                        macroPanel.Children.Add(new CheckBox()
                        {
                            IsEnabled = false, IsChecked = (macro.modifier & (1 << 2)) != 0, Content = "ALT"
                        });
                        macroPanel.Children.Add(new CheckBox()
                        {
                            IsEnabled = false, IsChecked = (macro.modifier & (1 << 3)) != 0, Content = "WIN"
                        });
                        for (int k = 0; k < 6; k++) // Key
                        {
                            macroPanel.Children.Add(new TextBlock()
                            {
                                Text = $"Key {k}: {Enum.ToObject(typeof(KeyboardKeys),macro.keys[k])}", Margin = new Thickness(10, 0, 10, 0)
                            });
                        }
                    }
                    rootStackPanel.Children.Add(macroPanel);
                }

                grid.Children.Add(rootStackPanel);
                root.Content = grid;
                TabControl.Items.Add(root);
            }
        }
 private void NewFile_OnClick(object sender, RoutedEventArgs e)
 {
     if (string.IsNullOrEmpty(fileLocation))
     {
         openFileDialog(false);
     }
     for (int i = 0; i < macroKeyDepth; i++)     // Macro
     {
         for (int j = 0; j < macroKeyDepth; j++) // Macro Keys
         {
             keyMacros[i, j] = new KeyMacro();
         }
     }
 }
        private void openFileDialog(bool openDialog)
        {
            FileDialog fileDialog;

            if (openDialog)
            {
                fileDialog       = new OpenFileDialog();
                fileDialog.Title = "Select file";
            }
            else
            {
                fileDialog = new SaveFileDialog();
            }


            fileDialog.CheckFileExists = false;
            fileDialog.Filter          = "Keyboard Macros (macros)|macros";
            if (fileDialog.ShowDialog() == true)
            {
                fileLocation = fileDialog.FileName;
            }

            if (!File.Exists(fileLocation))
            {
                File.Create(fileLocation).Close();
            }

            using (StreamReader reader = new StreamReader(fileLocation))
            {
                for (int i = 0; i < 9; i++)                 // Macro
                {
                    for (int j = 0; j < macroKeyDepth; j++) // Macro Keys
                    {
                        try
                        {
                            keyMacros[i, j].modifier = (byte)reader.BaseStream.ReadByte();
                            for (int k = 0; k < 6; k++) // Key
                            {
                                keyMacros[i, j].keys[k] = (byte)reader.BaseStream.ReadByte();
                            }
                        }
                        catch (Exception ex)
                        {
                            keyMacros[i, j] = new KeyMacro();
                        }
                    }
                }
            }
        }
        public EditCreateMacro()
        {
            InitializeComponent();
            for (int i = 1; i <= macroKeyDepth; i++)
            {
                var saveButton = new Button {
                    Name = "saveMacro" + i, Margin = new Thickness(0, 5, 0, 5), Content = "Save " + i
                };
                saveButton.Click += SaveMacro;
                var getButton = new Button {
                    Name = "getMacro" + i, Margin = new Thickness(0, 5, 0, 5), Content = "Get " + i
                };
                getButton.Click += GetMacro;
                savePanel.Children.Add(saveButton);
                getPanel.Children.Add(getButton);
//                            <Button x:Name="saveMacro1" Content="Save 1" Margin="0,5" Click="SaveMacro"></Button>
//                            <Button x:Name="getMacro1" Content="Get 1" Margin="0,5" Click="GetMacro"></Button>
            }
            for (int i = 0; i < 9; i++)                 // Macro
            {
                for (int j = 0; j < macroKeyDepth; j++) // Macro Keys
                {
                    keyMacros[i, j] = new KeyMacro();
                }
            }
            macroKeyCombo.SelectedIndex   = 0;
            macroKeyCombo.DropDownClosed += (sender, args) =>
                                            keyMacroComboPos = Convert.ToInt32(((ComboBox)sender).Text) - 1;

            cbxKey1.ItemsSource = Enum.GetValues(typeof(KeyboardKeys)).Cast <KeyboardKeys>();
            cbxKey2.ItemsSource = Enum.GetValues(typeof(KeyboardKeys)).Cast <KeyboardKeys>();
            cbxKey3.ItemsSource = Enum.GetValues(typeof(KeyboardKeys)).Cast <KeyboardKeys>();
            cbxKey4.ItemsSource = Enum.GetValues(typeof(KeyboardKeys)).Cast <KeyboardKeys>();
            cbxKey5.ItemsSource = Enum.GetValues(typeof(KeyboardKeys)).Cast <KeyboardKeys>();
            cbxKey6.ItemsSource = Enum.GetValues(typeof(KeyboardKeys)).Cast <KeyboardKeys>();

            cbxKeyC.ItemsSource = Enum.GetValues(typeof(ConsumerKeys)).Cast <ConsumerKeys>();

            cbxKey1.SelectedIndex = 0x00;
            cbxKey2.SelectedIndex = 0x00;
            cbxKey3.SelectedIndex = 0x00;
            cbxKey4.SelectedIndex = 0x00;
            cbxKey5.SelectedIndex = 0x00;
            cbxKey6.SelectedIndex = 0x00;

            cbxKeyC.SelectedIndex = 0x00;
        }
        private void SaveMacro(object sender, EventArgs e)
        {
            int macroPos = int.Parse(((Button)sender).Name.Substring(9)) - 1;

            if (rchecked(chbConsumer) == 1)
            {
                var keyMacro = new KeyMacro
                {
                    modifier = (byte)(0xff),
                    keys     =
                    {
                        [0] = (byte)((int)cbxKeyC.SelectedValue >> 8),
                        [1] = (byte)((int)cbxKeyC.SelectedValue),
                        [2] = (byte)0x00,
                        [3] = (byte)0x00,
                        [4] = (byte)0x00,
                        [5] = (byte)0x00
                    }
                };
                keyMacros[keyMacroComboPos, macroPos] = keyMacro;
            }
            else
            {
                var value = 0 | (rchecked(chbCtrl) << 0) | (rchecked(chbShift) << 1) |
                            (rchecked(chbAlt) << 2) | (rchecked(chbWin) << 3);
                var keyMacro = new KeyMacro
                {
                    modifier = (byte)(value),
                    keys     =
                    {
                        [0] = (byte)((int)cbxKey1.SelectedValue),
                        [1] = (byte)((int)cbxKey2.SelectedValue),
                        [2] = (byte)((int)cbxKey3.SelectedValue),
                        [3] = (byte)((int)cbxKey4.SelectedValue),
                        [4] = (byte)((int)cbxKey5.SelectedValue),
                        [5] = (byte)((int)cbxKey6.SelectedValue)
                    }
                };
                keyMacros[keyMacroComboPos, macroPos] = keyMacro;
            }
        }