Beispiel #1
0
        private void txtHotKey_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                var check     = false;
                var modString = "";
                var keyString = "";
                var hk        = new PNHotKey
                {
                    Modifiers = HotkeysStatic.GetModifiers(ref modString),
                    VK        = HotkeysStatic.GetKey(ref keyString),
                    Shortcut  = modString + keyString
                };

                if (hk.Modifiers == HotkeyModifiers.MOD_NONE)
                {
                    if (hk.VK >= HotkeysStatic.VK_F1 && hk.VK <= HotkeysStatic.VK_F24)
                    {
                        check = true;
                    }
                }
                else
                {
                    if (hk.VK > 0)
                    {
                        check = true;
                    }
                }
                if (!check)
                {
                    return;
                }
                string message, caption;
                if (PNStrings.RestrictedHotkeys.Contains(hk.Shortcut) ||
                    PNStatic.HotKeysEdit.Any(h => h.Modifiers == hk.Modifiers && h.VK == hk.VK) ||
                    PNStatic.HotKeysGroups.Any(h => h.Modifiers == hk.Modifiers && h.VK == hk.VK) ||
                    PNStatic.HotKeysNote.Any(h => h.Modifiers == hk.Modifiers && h.VK == hk.VK) ||
                    hk.Modifiers == HotkeyModifiers.MOD_SHIFT && (hk.VK >= HotkeysStatic.VK_A && hk.VK <= HotkeysStatic.VK_Z))
                {
                    message  = hk.Shortcut + '\n';
                    message += PNLang.Instance.GetMessageText("hk_registered_1", "This combination of keys is already registered.");
                    message += '\n';
                    message += PNLang.Instance.GetMessageText("hk_registered_2", "Choose another one, please.");
                    caption  = PNLang.Instance.GetCaptionText("restricted_keys", "Invalid keys");
                    PNMessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    e.Handled = true;
                    return;
                }
                if (HotkeysStatic.RegisterHK(_HwndSource.Handle, hk))
                {
                    HotkeysStatic.UnregisterHK(_HwndSource.Handle, hk.ID);
                }
                else
                {
                    message  = hk.Shortcut + '\n';
                    message += PNLang.Instance.GetMessageText("hk_registered_1", "This combination of keys is already registered.");
                    message += '\n';
                    message += PNLang.Instance.GetMessageText("hk_registered_2", "Choose another one, please.");
                    caption  = PNLang.Instance.GetCaptionText("restricted_keys", "Invalid keys");
                    PNMessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    e.Handled = true;
                    return;
                }
                PNTreeItem tvi       = null;
                PNHotKey   hkCurrent = null;
                switch (tabHK.SelectedIndex)
                {
                case 0:
                    tvi = tvwHKMain.SelectedItem as PNTreeItem;
                    if (tvi == null)
                    {
                        break;
                    }
                    hkCurrent = m_KeysMain.FirstOrDefault(h => h.MenuName == (string)tvi.Tag);
                    setButtonsEnabled(tvi);
                    break;

                case 1:
                    tvi = tvwHKNote.SelectedItem as PNTreeItem;
                    if (tvi == null)
                    {
                        break;
                    }
                    hkCurrent = m_KeysNote.FirstOrDefault(h => h.MenuName == (string)tvi.Tag);
                    setButtonsEnabled(tvi);
                    break;

                case 2:
                    tvi = tvwHKEdit.SelectedItem as PNTreeItem;
                    if (tvi == null)
                    {
                        break;
                    }
                    hkCurrent = m_KeysEdit.FirstOrDefault(h => h.MenuName == (string)tvi.Tag);
                    setButtonsEnabled(tvi);
                    break;

                case 3:
                    tvi = tvwHKGroups.SelectedItem as PNTreeItem;
                    if (tvi == null)
                    {
                        break;
                    }
                    hkCurrent = m_KeysGroups.FirstOrDefault(h => h.MenuName == (string)tvi.Tag);
                    setButtonsEnabled(tvi);
                    break;
                }
                if (hkCurrent != null)
                {
                    hkCurrent.Modifiers = hk.Modifiers;
                    hkCurrent.Shortcut  = hk.Shortcut;
                    hkCurrent.VK        = hk.VK;
                    setButtonsEnabled(tvi);
                    txtHotKey.Text = hk.Shortcut;
                    var defKey = new DefKey(hkCurrent.ID, hkCurrent.Type, hkCurrent.MenuName)
                    {
                        Icon =
                            isHotkeyInDatabase(
                                tabHK.SelectedIndex == 0
                                    ? PNStatic.HotKeysMain
                                    : (tabHK.SelectedIndex == 1
                                        ? PNStatic.HotKeysNote
                                        : (tabHK.SelectedIndex == 2 ? PNStatic.HotKeysEdit : PNStatic.HotKeysGroups)),
                                hk)
                                ? (BitmapSource)TryFindResource("check")
                                : null,
                        MenuRange =
                            tabHK.SelectedIndex == 0
                                ? tbpHKMain.Header.ToString()
                                : (tabHK.SelectedIndex == 1
                                    ? tbpHKNote.Header.ToString()
                                    : (tabHK.SelectedIndex == 2
                                        ? tbpHKEdit.Header.ToString()
                                        : tbpHKGroups.Header.ToString())),
                        MenuText =
                            tabHK.SelectedIndex < 3
                                ? PNLang.Instance.GetMenuText(
                                tabHK.SelectedIndex == 0
                                        ? "main_menu"
                                        : (tabHK.SelectedIndex == 1 ? "note_menu" : "edit_menu"), hkCurrent.MenuName,
                                hkCurrent.MenuName)
                                : hkCurrent.MenuName,
                        Shortcut = hkCurrent.Shortcut
                    };
                    if (tabHK.SelectedIndex == 3)
                    {
                        var arr = hkCurrent.MenuName.Split('_');
                        if (arr.Length == 2)
                        {
                            var gr = PNStatic.Groups.GetGroupByID(Convert.ToInt32(arr[0]));
                            if (gr != null)
                            {
                                var defCap = arr[1] == "show"
                                    ? PNLang.Instance.GetCaptionText("show_group", "Show group")
                                    : PNLang.Instance.GetCaptionText("hide_group", "Hide group");
                                defKey.MenuText = gr.Name + "/" + defCap;
                            }
                        }
                    }
                    //remove existing key
                    var existingKey = _DefKeys.FirstOrDefault(k => k.Id == defKey.Id);
                    if (existingKey != null)
                    {
                        _DefKeys.Remove(existingKey);
                    }
                    _DefKeys.Add(defKey);
                }
            }
            catch (Exception ex)
            {
                PNStatic.LogException(ex);
            }
        }
Beispiel #2
0
        private bool saveHotkeys()
        {
            try
            {
                var sqlList = new List <string>();

                var count = m_KeysMain.Count;
                for (var i = 0; i < count; i++)
                {
                    if (PNStatic.HotKeysMain[i] == m_KeysMain[i])
                    {
                        continue;
                    }
                    //first unregister existing hot key
                    if (PNStatic.HotKeysMain[i].VK != 0)
                    {
                        HotkeysStatic.UnregisterHK(PNStatic.FormMain.Handle, PNStatic.HotKeysMain[i].ID);
                    }
                    //if new hot key has been set
                    if (m_KeysMain[i].VK != 0)
                    {
                        //now try to register
                        if (!HotkeysStatic.RegisterHK(PNStatic.FormMain.Handle, m_KeysMain[i]))
                        {
                            continue;
                        }
                        //copy hot key
                        PNStatic.HotKeysMain[i].CopyFrom(m_KeysMain[i]);
                        var sqlQuery = createHotKeyUpdate(m_KeysMain[i]);
                        if (sqlQuery != "")
                        {
                            sqlList.Add(sqlQuery);
                        }
                    }
                    else
                    {
                        //hot key has been deleted
                        PNStatic.HotKeysMain[i].CopyFrom(m_KeysMain[i]);
                        var sqlQuery = createHotKeyDelete(m_KeysMain[i]);
                        if (sqlQuery != "")
                        {
                            sqlList.Add(sqlQuery);
                        }
                    }
                }

                count = m_KeysNote.Count;
                for (int i = 0; i < count; i++)
                {
                    if (PNStatic.HotKeysNote[i] == m_KeysNote[i])
                    {
                        continue;
                    }

                    //first unregister existing hot key
                    if (PNStatic.HotKeysNote[i].VK != 0)
                    {
                        HotkeysStatic.UnregisterHK(PNStatic.FormMain.Handle, PNStatic.HotKeysNote[i].ID);
                    }

                    //if new hot key has been set
                    if (m_KeysNote[i].VK != 0)
                    {
                        //now try to register
                        if (!HotkeysStatic.RegisterHK(PNStatic.FormMain.Handle, m_KeysNote[i]))
                        {
                            continue;
                        }
                        //copy hot key
                        PNStatic.HotKeysNote[i].CopyFrom(m_KeysNote[i]);
                        var sqlQuery = createHotKeyUpdate(m_KeysNote[i]);
                        if (sqlQuery != "")
                        {
                            sqlList.Add(sqlQuery);
                        }
                    }
                    else
                    {
                        //hot key has been deleted
                        PNStatic.HotKeysNote[i].CopyFrom(m_KeysNote[i]);
                        var sqlQuery = createHotKeyDelete(m_KeysNote[i]);
                        if (sqlQuery != "")
                        {
                            sqlList.Add(sqlQuery);
                        }
                    }

                    ////copy hot key
                    //PNStatic.HotKeysNote[i].CopyFrom(m_KeysNote[i]);
                    //var sqlQuery = m_KeysNote[i].VK != 0
                    //    ? createHotKeyUpdate(m_KeysNote[i])
                    //    : createHotKeyDelete(m_KeysNote[i]);
                    //if (sqlQuery != "")
                    //{
                    //    sqlList.Add(sqlQuery);
                    //}
                }

                count = m_KeysEdit.Count;
                for (int i = 0; i < count; i++)
                {
                    if (PNStatic.HotKeysEdit[i] == m_KeysEdit[i])
                    {
                        continue;
                    }

                    //first unregister existing hot key
                    if (PNStatic.HotKeysEdit[i].VK != 0)
                    {
                        HotkeysStatic.UnregisterHK(PNStatic.FormMain.Handle, PNStatic.HotKeysEdit[i].ID);
                    }
                    //if new hot key has been set
                    if (m_KeysEdit[i].VK != 0)
                    {
                        //now try to register
                        if (!HotkeysStatic.RegisterHK(PNStatic.FormMain.Handle, m_KeysEdit[i]))
                        {
                            continue;
                        }
                        //copy hot key
                        PNStatic.HotKeysEdit[i].CopyFrom(m_KeysEdit[i]);
                        var sqlQuery = createHotKeyUpdate(m_KeysEdit[i]);
                        if (sqlQuery != "")
                        {
                            sqlList.Add(sqlQuery);
                        }
                    }
                    else
                    {
                        //hot key has been deleted
                        PNStatic.HotKeysEdit[i].CopyFrom(m_KeysEdit[i]);
                        var sqlQuery = createHotKeyDelete(m_KeysEdit[i]);
                        if (sqlQuery != "")
                        {
                            sqlList.Add(sqlQuery);
                        }
                    }

                    ////copy hot key
                    //PNStatic.HotKeysEdit[i].CopyFrom(m_KeysEdit[i]);
                    //var sqlQuery = m_KeysEdit[i].VK != 0
                    //    ? createHotKeyUpdate(m_KeysEdit[i])
                    //    : createHotKeyDelete(m_KeysEdit[i]);
                    //if (sqlQuery != "")
                    //{
                    //    sqlList.Add(sqlQuery);
                    //}
                }

                count = m_KeysGroups.Count;
                for (var i = 0; i < count; i++)
                {
                    if (PNStatic.HotKeysGroups[i] == m_KeysGroups[i])
                    {
                        continue;
                    }
                    //first unregister existing hot key
                    if (PNStatic.HotKeysGroups[i].VK != 0)
                    {
                        HotkeysStatic.UnregisterHK(PNStatic.FormMain.Handle, PNStatic.HotKeysGroups[i].ID);
                    }
                    //if new hot key has been set
                    if (m_KeysGroups[i].VK != 0)
                    {
                        //now try to register
                        if (!HotkeysStatic.RegisterHK(PNStatic.FormMain.Handle, m_KeysGroups[i]))
                        {
                            continue;
                        }
                        //copy hot key
                        PNStatic.HotKeysGroups[i].CopyFrom(m_KeysGroups[i]);
                        var sqlQuery = createHotKeyUpdate(m_KeysGroups[i]);
                        if (sqlQuery != "")
                        {
                            sqlList.Add(sqlQuery);
                        }
                    }
                    else
                    {
                        //hot key has been deleted
                        PNStatic.HotKeysGroups[i].CopyFrom(m_KeysGroups[i]);
                        var sqlQuery = createHotKeyDelete(m_KeysGroups[i]);
                        if (sqlQuery != "")
                        {
                            sqlList.Add(sqlQuery);
                        }
                    }
                }

                if (sqlList.Count <= 0)
                {
                    return(false);
                }
                using (var oData = new SQLiteDataObject(PNData.ConnectionString))
                {
                    foreach (var s in sqlList)
                    {
                        oData.Execute(s);
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                PNStatic.LogException(ex);
                return(false);
            }
        }