コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: lot224/SoundBoard
 private void KeyUpHandler(object sender, KeyEventArgs e)
 {
     // if a focus gets messed up, re-focus it here
     if (focusedButton != null)
     {
         focusedButton.Focus();
     }
 }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: lot224/SoundBoard
        private void KeyDownHandler(object sender, KeyEventArgs e)
        {
            Mouse.Capture(null);

            char c = GetCharFromKey(e.Key);

            if (char.IsLetter(c) || char.IsPunctuation(c) || char.IsNumber(c))
            {
                searchString += c;
            }
            else if (e.Key == Key.Space)
            {
                searchString += ' ';
            }
            else if (e.Key == Key.Back && searchString.Length > 0)
            {
                searchString = searchString.Substring(0, searchString.Length - 1);
            }
            else if (e.Key == Key.Escape)
            {
                // if the search bar is open, close it
                if (Search.IsOpen)
                {
                    searchString  = ""; // don'even wait for it to close to clear the query
                    Search.IsOpen = false;
                }
                // otherwise, stop any playing sounds
                else
                {
                    ButtonAutomationPeer peer       = new ButtonAutomationPeer(Silence);
                    IInvokeProvider      invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
                    invokeProv.Invoke();
                }
                return;
            }
            else if (e.Key == Key.Down)
            {
                bool foundFocused = false;
                // loop through the buttons and focus the next one
                foreach (var child in ResultsPanel.Children)
                {
                    if (child is SoundButton)
                    {
                        if (foundFocused)
                        {
                            // we found the last focused button, so focus this one
                            ((SoundButton)child).Focus();
                            focusedButton = ((SoundButton)child);
                            break;
                        }
                        if (((SoundButton)child).IsFocused)
                        {
                            // we found the focused button! focus the next one
                            foundFocused = true;
                        }
                    }
                }
                return;
            }
            else if (e.Key == Key.Up)
            {
                SoundButton previousButton = null;
                // loop through the buttons and focus the previous one
                foreach (var child in ResultsPanel.Children)
                {
                    if (child is SoundButton)
                    {
                        if (((SoundButton)child).IsFocused)
                        {
                            // focus the previous one!
                            if (previousButton != null)
                            {
                                previousButton.Focus();
                                focusedButton = previousButton;
                                break;
                            }
                        }
                        previousButton = (SoundButton)child;
                    }
                }
                return;
            }
            else if (e.Key == Key.Enter)
            {
                // play the sound!
                foreach (var child in ResultsPanel.Children)
                {
                    if (child is SoundButton && ((SoundButton)child).IsFocused)
                    {
                        ButtonAutomationPeer peer       = new ButtonAutomationPeer((SoundButton)child);
                        IInvokeProvider      invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
                        invokeProv.Invoke();
                    }
                }
                return;
            }
            else
            {
                return;
            }

            Query.Text       = searchString;
            Query.CaretIndex = Query.Text.Length;

            Search.IsOpen = true;

            // get rid of any previous buttons / do reverse iteration to prevent weird bugs
            for (int i = ResultsPanel.Children.Count - 1; i >= 0; --i)
            {
                if (ResultsPanel.Children[i] is SoundButton)
                {
                    ResultsPanel.Children.Remove((SoundButton)ResultsPanel.Children[i]);
                }
            }

            // perform search
            if (searchString != "")
            {
                foreach (KeyValuePair <string, string> entry in sounds)
                {
                    if (entry.Key.ToLower().Contains(searchString.ToLower()))
                    {
                        SoundButton button = new SoundButton(true);
                        button.SetFile(entry.Value, entry.Key, false);

                        ResultsPanel.Children.Add(button);
                    }
                }

                // if we've added at least one button, focus the first one
                if (ResultsPanel.Children.Count > 0)
                {
                    ((SoundButton)ResultsPanel.Children[0]).Focus();
                    focusedButton = ((SoundButton)ResultsPanel.Children[0]);
                }
            }
        }