Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

KeyAction.cpp

Go to the documentation of this file.
00001 #include "KeyAction.h"
00002 
00003 KeyAction::KeyAction()
00004 {
00005         qDebug("KeyAction::KeyAction()");
00006         enable();
00007 }
00008 
00009 KeyAction::~KeyAction()
00010 {
00011         qDebug("KeyAction::~KeyAction()");
00012 }
00013 
00014 void KeyAction::init()
00015 {
00016         ConfigEx& cfg = ConfigEx::getInstance("keyhelper");
00017 
00018         QString oldgroup = cfg.getGroup();
00019         cfg.setGroup("Global");
00020 
00021         m_keepToggle = cfg.readBoolEntry("KeepToggle", true);
00022 
00023         m_excludeKeys.clear();
00024         if(cfg.hasKey("ExcludeKeys")){
00025                 QStringList keys = cfg.readListEntry("ExcludeKeys", ',');
00026                 for(QStringList::Iterator it=keys.begin();
00027                                 it!=keys.end(); ++it){
00028                         int code;
00029                         if((*it).find("0x") == 0){
00030                                 code = KHUtil::hex2int(*it);
00031                         } else {
00032                                 code = KeyNames::getCode(*it);
00033                         }
00034                         if(code > 0){
00035                                 m_excludeKeys.append(code);
00036                         }
00037                 }
00038         } else {
00039                 m_excludeKeys.append(Qt::Key_F34);
00040         }
00041 
00042         m_presscnt = 0;
00043         m_check = true;
00044         m_keep_toggle_code = 0;
00045         m_capture = false;
00046         cfg.setGroup(oldgroup);
00047 }
00048 
00049 void KeyAction::setAction(int unicode, int keycode, int modifiers,
00050                 bool isPress, bool autoRepeat)
00051 {
00052         m_unicode = unicode;
00053         m_keycode = keycode;
00054         m_modifiers = modifiers;
00055         m_isPress = isPress;
00056         m_autoRepeat = autoRepeat;
00057 }
00058 
00059 bool KeyAction::checkState()
00060 {
00061         if(0 < m_unicode && m_unicode < 0xFFFF){
00062                 QChar ch(m_unicode);
00063                 QChar::Category category = ch.category();
00064                 if(category == QChar::Letter_Lowercase){
00065                         if(m_modifiers == 0){
00066                                 m_pMappings->setCapsLock(false);
00067                                 return(true);
00068                         } else if(m_modifiers == Qt::ShiftButton){
00069                                 m_pMappings->setCapsLock(true);
00070                                 return(true);
00071                         }
00072                 } else if(category == QChar::Letter_Uppercase){
00073                         if(m_modifiers == 0){
00074                                 m_pMappings->setCapsLock(true);
00075                                 return(true);
00076                         } else if(m_modifiers == Qt::ShiftButton){
00077                                 m_pMappings->setCapsLock(false);
00078                                 return(true);
00079                         }
00080                 }
00081         }
00082         return(false);
00083 }
00084 
00085 void KeyAction::sendKeyEvent(int unicode, int keycode, int modifiers,
00086                 bool isPress, bool autoRepeat)
00087 {
00088         if(m_hookChannel.isEmpty()){
00089                         QWSServer::sendKeyEvent(unicode, keycode, modifiers, isPress, autoRepeat);
00090         } else {
00091                 if(QCopChannel::isRegistered(m_hookChannel)){
00092                         QCopEnvelope e(m_hookChannel, "keyEvent(int,int,int,int,int)");
00093                         e << unicode << keycode << modifiers << (int)isPress << (int)autoRepeat;
00094                 } else {
00095                         m_hookChannel = "";
00096                         QWSServer::sendKeyEvent(unicode, keycode, modifiers, isPress, autoRepeat);
00097                 }
00098         }
00099 }
00100 
00101 bool KeyAction::doAction()
00102 {
00103         if(m_enable == false){
00104                 return(false);
00105         }
00106 #if 0
00107         if(m_excludeKeys.contains(m_keycode)){
00108                 return(false);
00109         }
00110 #endif
00111         if(!m_autoRepeat){
00112                 qDebug("recv[%x][%x][%x][%d]",
00113                         m_unicode,
00114                         m_keycode,
00115                         m_modifiers,
00116                         m_isPress);
00117         }
00118 
00119         if(m_autoRepeat && !m_excludeKeys.contains(m_keycode)){
00120                 KeyRepeater::RepeaterMode repMode = m_pRepeater->getMode();
00121                 if(repMode == KeyRepeater::ENABLE
00122                         || repMode == KeyRepeater::KILL){
00123                         /* autoRepeat は無視 */
00124                         return(true);
00125                 }
00126         }
00127 
00128         if(m_pRepeater->isRepeatable(m_keycode)){
00129                 if(m_isPress){
00130                         m_presscnt++;
00131                 } else {
00132                         m_presscnt--;
00133                         if(m_presscnt <= 0){
00134                                 m_pRepeater->stop();
00135                                 m_presscnt = 0;
00136                         }
00137                 }
00138         }
00139 
00140         if(m_check && m_isPress){
00141                 /* check capslock state */
00142                 if(checkState()){
00143                         m_check = false;
00144                 }
00145         }
00146 
00147         int unicode, keycode, modifiers;
00148         /* keep toggle reset */
00149         if(m_keepToggle && m_keep_toggle_code != 0){
00150                 if(m_keep_toggle_code != m_keycode){
00151                         m_pModifiers->resetToggles();
00152                         m_keep_toggle_code = 0;
00153                 } else {
00154                         m_pModifiers->keepToggles();
00155                 }
00156         }
00157 
00158         /* modifier 状態取得 */
00159         int keymask = m_pModifiers->getState(m_modifiers);
00160         modifiers = m_pModifiers->getModifiers(m_modifiers);
00161 
00162         bool isModMapped = false;
00163         /* modifier 状態更新 */
00164         if(m_autoRepeat == false){
00165                 if(m_isPress){
00166                         isModMapped = m_pModifiers->pressKey(m_keycode, m_modifiers);
00167                 } else {
00168                         m_pModifiers->releaseKey(m_keycode);
00169                 }
00170         }
00171 
00172         if(m_capture && m_isPress && m_autoRepeat == false){
00173                 QCopEnvelope e("QPE/KeyHelperConf", "event(int,int,int)");
00174                 e << m_keycode << m_unicode << modifiers;
00175         }
00176 
00177         /* keyextension 適用 */
00178         bool fKeyCancel = m_pExtensions->doKey(m_keycode, keymask, m_isPress);
00179         if(fKeyCancel){
00180                 if(m_keepToggle){
00181                         m_keep_toggle_code = m_keycode;
00182                 } else {
00183                         m_pModifiers->resetToggles();
00184                 }
00185                 m_pRepeater->stop();
00186                 return(true);
00187         }
00188 
00189         /* keymapping 適用 */
00190 #if 0
00191         bool isMapped = m_pMappings->apply(m_keycode, keymask, m_isPress);
00192         if(isMapped == false){
00193                 if(m_pMappings->isDefined()){
00194                         m_pMappings->setOriginal(m_unicode, m_modifiers);
00195                 } else {
00196                         m_pMappings->setUnicode(m_unicode);
00197                         //m_pMappings->setKeycode(m_keycode);
00198                 }
00199                 //return(false);
00200         }
00201 #else
00202         bool isMapped = m_pMappings->apply(m_unicode, m_keycode, m_modifiers,
00203                                 keymask, m_isPress);
00204 #endif
00205 
00206         /* modifier 適用 */
00207         m_pMappings->setModifiers(modifiers);
00208 
00209         keycode = m_pMappings->getKeycode();
00210         if(keycode <= 0){
00211                 return(true);
00212         }
00213 
00214         if(/*m_autoRepeat == false &&*/ m_isPress){
00215                 if(isModMapped == false || (isModMapped && isMapped)){
00216                         if(m_pModifiers->isModifier(keycode) == false){
00217                                 m_pModifiers->resetToggles();
00218                         }
00219                 }
00220         }
00221         unicode = m_pMappings->getUnicode();
00222         modifiers = m_pMappings->getModifiers();
00223         if(keycode > 0){
00224                 /* disable mapping */
00225                 if(m_excludeKeys.contains(keycode)){
00226                         return(false);
00227                 }
00228                 /* send key event */
00229                 sendKeyEvent(
00230                         unicode,
00231                         keycode,
00232                         modifiers,
00233                         m_isPress,
00234                         m_autoRepeat);
00235         }
00236         if(m_isPress){
00237                 /* repeater start */
00238                 m_pRepeater->start(unicode, keycode, modifiers);
00239 #if 0
00240         } else if(m_presscnt <= 0){
00241                 m_presscnt = 0;
00242                 m_pRepeater->stop();
00243 #endif
00244         } else {
00245                 m_pRepeater->stop(keycode);
00246         }
00247         qWarning("send[%x][%x][%x][%d]",
00248                 m_pMappings->getUnicode(),
00249                 m_pMappings->getKeycode(),
00250                 m_pMappings->getModifiers(),
00251                 m_isPress);
00252         return(true);
00253 }

Generated on Sat Nov 5 16:16:43 2005 for OPIE by  doxygen 1.4.2