-- 清除所以快捷键的按键绑定
local function ClearBindingKey()
	for i = 1, GetNumBindings() do
		local action, _, _, _ = GetBinding(i)
		local key1, key2 = GetBindingKey(action)
        if key1 then
            SetBinding(key1, nil)
        end
        if key2 then
            SetBinding(key2, nil)
        end
	end
end


-- 设置快捷键
local function SetBindingKey()

	-- 清除所以快捷键绑定
	ClearBindingKey()
	
	local variable = {
		{key = "W", action = "MOVEFORWARD"}, 						-- 前进
		{key = "S", action = "MOVEBACKWARD"}, 						-- 后退
		{key = "A", action = "TURNLEFT"}, 							-- 左转
		{key = "D", action = "TURNRIGHT"}, 							-- 右转
		{key = "SPACE", action = "JUMP"}, 							-- 跳跃
		{key = "ESCAPE", action = "TOGGLEGAMEMENU"}, 				-- 打开/关闭游戏菜单
		{key = "B", action = "OPENALLBAGS"}, 						-- 打开/关闭所有的背包
		{key = "U", action = "TOGGLECHARACTER0"}, 					-- 打开/关闭角色界面
		{key = "", action = "TOGGLECHARACTER2"}, 					-- 打开/关闭声望界面
		{key = "P", action = "TOGGLESPELLBOOK"}, 					-- 打开/关闭法术书
		{key = "N", action = "TOGGLETALENTS"}, 						-- 打开/关闭天赋面板
		{key = "Y", action = "TOGGLEACHIEVEMENT"}, 					-- 打开/关闭成就面板
		{key = "L", action = "TOGGLEQUESTLOG"}, 					-- 打开/关闭任务日志
		{key = "M", action = "TOGGLEWORLDMAP"}, 					-- 打开/关闭世界地图
		{key = "J", action = "TOGGLEGUILDTAB"}, 					-- 打开/关闭公会和社区
		{key = "O", action = "TOGGLESOCIAL"}, 						-- 打开/关闭社交界面
		{key = "I", action = "TOGGLEGROUPFINDER"}, 					-- 打开/关闭队伍查找器
		{key = "", action = "TOGGLECHARACTER4"}, 					-- 打开/关闭PvP面板
		{key = "SHIFT-P", action = "TOGGLECOLLECTIONS"}, 			-- 打开/关闭藏品窗口
		{key = "SHIFT-J", action = "TOGGLEENCOUNTERJOURNAL"}, 		-- 打开/关闭冒险指南
		{key = "SHIFT-K", action = "TOGGLEGARRISONLANDINGPAGE"}, 	-- 打开/关闭要塞报告
		{key = "SHIFT-M", action = "TOGGLEBATTLEFIELDMINIMAP"}, 	-- 打开/关闭区域地图开关
		{key = "K", action = "TOGGLEPROFESSIONBOOK"}, 				-- 打开/关闭专业技能书
		{key = "1", action = "ACTIONBUTTON1"}, 						-- 快捷键1
		{key = "2", action = "ACTIONBUTTON2"}, 						-- 快捷键2
		{key = "3", action = "ACTIONBUTTON3"}, 						-- 快捷键3
		{key = "4", action = "ACTIONBUTTON4"}, 						-- 快捷键4
		{key = "5", action = "ACTIONBUTTON5"}, 						-- 快捷键5
		{key = "6", action = "ACTIONBUTTON6"}, 						-- 快捷键6
		{key = "7", action = "ACTIONBUTTON7"}, 						-- 快捷键7
		{key = "8", action = "ACTIONBUTTON8"}, 						-- 快捷键8
		{key = "9", action = "ACTIONBUTTON9"}, 						-- 快捷键9
		{key = "0", action = "ACTIONBUTTON10"}, 					-- 快捷键10
		{key = "-", action = "ACTIONBUTTON11"}, 					-- 快捷键11
		{key = "=", action = "ACTIONBUTTON12"}, 					-- 快捷键12
		{key = "Z", action = "MULTIACTIONBAR1BUTTON1"}, 			-- 动作条2快捷键1
		{key = "X", action = "MULTIACTIONBAR1BUTTON2"}, 			-- 动作条2快捷键2
		{key = "C", action = "MULTIACTIONBAR1BUTTON3"}, 			-- 动作条2快捷键3
		{key = "F", action = "MULTIACTIONBAR1BUTTON4"}, 			-- 动作条2快捷键4
		{key = "G", action = "MULTIACTIONBAR1BUTTON5"}, 			-- 动作条2快捷键5
		{key = "H", action = "MULTIACTIONBAR1BUTTON6"}, 			-- 动作条2快捷键6
		{key = "Q", action = "MULTIACTIONBAR1BUTTON7"}, 			-- 动作条2快捷键7
		{key = "E", action = "MULTIACTIONBAR1BUTTON8"}, 			-- 动作条2快捷键8
		{key = "T", action = "MULTIACTIONBAR1BUTTON9"}, 			-- 动作条2快捷键9
		{key = "`", action = "MULTIACTIONBAR1BUTTON10"}, 			-- 动作条2快捷键10
		{key = "MOUSEWHEELUP", action = "MULTIACTIONBAR1BUTTON11"}, 			-- 动作条2快捷键11
		{key = "MOUSEWHEELDOWN", action = "MULTIACTIONBAR1BUTTON12"}, 			-- 动作条2快捷键12
		{key = "PAGEUP", action = "MULTIACTIONBAR2BUTTON1"}, 		-- 动作条3快捷键1
		{key = "]", action = "TOGGLEPINGLISTENER"}, 		-- 
		{key = "[", action = "TOGGLECOLLECTIONSMOUNTJOURNAL"}, 		-- 
		{key = "ENTER", action = "OPENCHAT"}, 						-- 打开对话框
		{key = "/", action = "OPENCHATSLASH"}, 						-- 打开带“/”的对话框
		{key = "TAB", action = "TARGETNEARESTENEMY"}, 				-- 选中最近的敌人
		{key = "V", action = "NAMEPLATES"}, 						-- 显示敌方姓名板
		{key = "SHIFT-V", action = "FRIENDNAMEPLATES"}, 			-- 显示友方姓名板
		{key = "CTRL-V", action = "ALLNAMEPLATES"}, 				-- 显示所有姓名板
		{key = "PAGEDOWN", action = "INTERACTTARGET"}, 					-- 与目标互动
		{key = "BUTTON4", action = "INTERACTTARGET"}, 					-- 与目标互动
		{key = "MOUSEWHEELUP", action = "CAMERAZOOMIN"}, 			-- 放大
		{key = "MOUSEWHEELDOWN", action = "CAMERAZOOMOUT"}, 		-- 缩小
		{key = "PRINTSCREEN", action = "SCREENSHOT"},				-- 截图	
		{key = "BUTTON5", action = "TOGGLEAUTORUN"},				-- 自动奔跑	
	}
	
	for _, v in pairs(variable) do
		SetBinding(v.key, v.action)
	end
	
	-- 保存 0 = 默认, 1 = 账户, 2 = 角色
	SaveBindings(1)
end

SlashCmdList["SBK"] = SetBindingKey
SLASH_SBK1 = "/sbk"
local UpdateBingdingCvarButton = CreateFrame("Button", nil, SettingsPanel, "UIPanelButtonTemplate")
UpdateBingdingCvarButton:SetPoint("BOTTOMLEFT", SettingsPanel, 405, 16)
UpdateBingdingCvarButton:SetSize(140, 22)
UpdateBingdingCvarButton:SetText("快捷键推荐设置")
UpdateBingdingCvarButton:SetScript("OnClick", function()
    SetBindingKey()
end)