⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hotkeymanager.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.hotkey;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;

/**
 * 热键管理器
 * 
 * @author luma
 */
public class HotkeyManager {
    // 热键注册表,key是热键的值,value是事件ID
    private Map hotkeys;
    // 热键监听器
    private List listeners;
    // 热键是否能使用
    private boolean ready;
    
    // 单一实例
    private static HotkeyManager instance;
    
    /**
     * @return
     * 		单一实例
     */
    public static HotkeyManager getInstance() {
        if(instance == null)
            instance = new HotkeyManager();
        return instance;
    }
    
    /**
     * 私有构造函数
     */
    private HotkeyManager() {
        hotkeys = new HashMap();
        listeners = new ArrayList();        
        try {
            System.loadLibrary("hotkey");
            init();
            ready = true;
        } catch (Throwable e) {
            ready = false;            
        }
    }
    
    /**
     * 添加热键
     * 
     * @param code
     * 		key code
     * @param modifier
     * 		modifier
     * @return
     * 		true表示添加热键成功
     */
    private native boolean grabKey(int code, int modifier);
    
    /**
     * 注销热键
     * 
     * @param code
     * 		key code
     * @param modifier
     * 		modifier
     * @param
     * 		true表示注销热键成功
     */
    private native boolean freeKey(int code, int modifier);
    
    /**
     * 初始化热键程序
     */
    private native void init();
    
    /**
     * 添加热键监听器
     * 
     * @param listener
     * 		IHotkeyListener对象
     */
    public void addHotkeyListener(IHotkeyListener listener) {
        listeners.add(listener);
    }
    
    /**
     * 删除一个热键监听器
     * 
     * @param listener
     * 		IHotkeyListener对象
     */
    public void removeHotkeyListener(IHotkeyListener listener) {
        listeners.remove(listener);
    }
    
    /**
     * 某个热键被按下,此方法供jni回调
     * 
     * @param code
     * @param modifier
     */
    public void keyPressed(int code, int modifier) {        
        int key = code | modifier;
        Integer i = new Integer(key);
        if(!hotkeys.containsKey(i))
            return;
        
        int action = ((Integer)hotkeys.get(i)).intValue();
        int size = listeners.size();
        for(int p = 0; p < size; p++)
            ((IHotkeyListener)listeners.get(p)).hotkeyPressed(action);
    }
    
    /**
     * 注册热键
     * 
     * @param key
     * 		热键键值
     * @param action
     * 		触发事件
     */
    public void addHotkey(int key, int action) {
        if(!ready)
            return;
        
        Integer i = new Integer(key);
        if(hotkeys.containsKey(i))
            return;
        
		int keyValue = key & SWT.KEY_MASK;
		int modifierValue = key & SWT.MODIFIER_MASK;
		char keyChar = (char)keyValue;
		
		if(grabKey(keyValue, modifierValue))
		    hotkeys.put(i, new Integer(action));
    }
    
    /**
     * 删除一个热键
     * 
     * @param key
     * 		热键键值
     */
    public void removeHotkey(int key) {
        if(!ready)
            return;
        
        Integer i = new Integer(key);
        if(!hotkeys.containsKey(i))
            return;
        
		int keyValue = key & SWT.KEY_MASK;
		int modifierValue = key & SWT.MODIFIER_MASK;
		char keyChar = (char)keyValue;
		
		if(freeKey(keyValue, modifierValue))
		    hotkeys.remove(i);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -