public static bool SetShortCut(this RibbonCommandItem commandItem, string key) { try { if (commandItem == null || string.IsNullOrEmpty(key)) { return(false); } RibbonTab parentTab; RibbonPanel parentPanel; var commandId = ControlHelper.GetCommandId(commandItem); if (string.IsNullOrEmpty(commandId)) { commandId = Guid.NewGuid().ToString(); ControlHelper.SetCommandId(commandItem, commandId); } ComponentManager.Ribbon.FindItem(commandItem.Id, false, out parentPanel, out parentTab, true); if (parentTab == null || parentPanel == null) { return(false); } var path = $"{parentTab.Id}>{parentPanel.Source.Id}"; var shortcutItem = new ShortcutItem(commandItem.Text, commandId, key, path) { ShortcutType = StType.RevitAPI }; KeyboardShortcutService.applyShortcutChanges( new Dictionary <string, ShortcutItem> { { commandId, shortcutItem } }); return(true); } catch (Exception) { return(false); } }
/// <summary> /// 给指定按钮设置快捷键 /// </summary> /// <param name="commandItem"></param> /// <param name="key"></param> /// <returns></returns> public static bool SetShortCut(this RibbonCommandItem commandItem, string key) { try { if (commandItem == null || string.IsNullOrEmpty(key)) { return(false); } var parentTab = default(Autodesk.Windows.RibbonTab); var parentPanel = default(Autodesk.Windows.RibbonPanel); Autodesk.Windows.ComponentManager.Ribbon.FindItem(commandItem.Id, false, out parentPanel, out parentTab, true); if (parentTab == null || parentPanel == null) { return(false); } var path = string.Format("{0}>{1}", parentTab.Id, parentPanel.Source.Id); var commandId = ControlHelper.GetCommandId(commandItem); if (string.IsNullOrEmpty(commandId)) { commandId = Guid.NewGuid().ToString(); ControlHelper.SetCommandId(commandItem, commandId); } var shortcutItem = new ShortcutItem(commandItem.Text, commandId, key, path); shortcutItem.ShortcutType = StType.RevitAPI; KeyboardShortcutService.applyShortcutChanges( new Dictionary <string, ShortcutItem>() { { commandId, shortcutItem } } ); return(true); } catch (Exception) { return(false); } }