camerauis/cameraxui/cxui/src/cxuicapturekeyhandler.cpp
changeset 28 3075d9b614e6
parent 19 d9aefe59d544
child 45 24fd82631616
equal deleted inserted replaced
19:d9aefe59d544 28:3075d9b614e6
    42 
    42 
    43     // Capture keys for primary camera
    43     // Capture keys for primary camera
    44     mEngine.featureManager().configuredValues(CxeRuntimeKeys::PRIMARY_CAMERA_CAPTURE_KEYS,
    44     mEngine.featureManager().configuredValues(CxeRuntimeKeys::PRIMARY_CAMERA_CAPTURE_KEYS,
    45                                               mPrimaryCameraCaptureKeys);
    45                                               mPrimaryCameraCaptureKeys);
    46 
    46 
    47     int scanCode = 0;
    47     listenKeys(true);
    48     int handle = 0;
       
    49     foreach (scanCode, mPrimaryCameraAutofocusKeys) {
       
    50         handle = mWindowGroup.CaptureKeyUpAndDowns(scanCode, 0, 0, CXUI_KEY_PRIORITY);
       
    51         mCapturedKeyUpDownHandles.append(handle);
       
    52     }
       
    53     foreach (scanCode, mPrimaryCameraCaptureKeys) {
       
    54         handle = mWindowGroup.CaptureKeyUpAndDowns(scanCode, 0, 0, CXUI_KEY_PRIORITY);
       
    55         mCapturedKeyUpDownHandles.append(handle);
       
    56     }
       
    57 
       
    58     // Capture key press events... this is done only to make sure other
       
    59     // applications do not react to camera key events.
       
    60     handle = mWindowGroup.CaptureKey(EKeyCamera, 0, 0, CXUI_KEY_PRIORITY);
       
    61     mCapturedKeyHandles.append(handle);
       
    62 
       
    63     CX_DEBUG_EXIT_FUNCTION();
    48     CX_DEBUG_EXIT_FUNCTION();
    64 }
    49 }
    65 
    50 
    66 CxuiCaptureKeyHandler::~CxuiCaptureKeyHandler()
    51 CxuiCaptureKeyHandler::~CxuiCaptureKeyHandler()
    67 {
    52 {
    68     CX_DEBUG_ENTER_FUNCTION();
    53     CX_DEBUG_ENTER_FUNCTION();
       
    54     listenKeys(false);
       
    55     CX_DEBUG_EXIT_FUNCTION();
       
    56 }
    69 
    57 
    70     int handle = 0;
    58 /*!
    71     foreach (handle, mCapturedKeyUpDownHandles) {
    59 * Start or stop listening key events.
    72         mWindowGroup.CancelCaptureKeyUpAndDowns(handle);
    60 * @param listen Should we start (true) or stop (false) listening key events.
       
    61 */
       
    62 void CxuiCaptureKeyHandler::listenKeys(bool listen)
       
    63 {
       
    64     CX_DEBUG_ENTER_FUNCTION();
       
    65 
       
    66     if (listen) {
       
    67         // Protect from multiple calls
       
    68         if (mCapturedKeyUpDownHandles.empty() && mCapturedKeyHandles.empty()) {
       
    69 
       
    70             int key(0);
       
    71             foreach (key, mPrimaryCameraAutofocusKeys) {
       
    72                 CX_DEBUG(("CxuiCaptureKeyHandler - hooking autofocus key with scan / key code: %d", key));
       
    73                 listenKey(key);
       
    74             }
       
    75             foreach (key, mPrimaryCameraCaptureKeys) {
       
    76                 CX_DEBUG(("CxuiCaptureKeyHandler - hooking capture key with scan / key code: %d", key));
       
    77                 listenKey(key);
       
    78             }
       
    79         }
       
    80     } else {
       
    81 
       
    82         int handle(0);
       
    83         foreach (handle, mCapturedKeyUpDownHandles) {
       
    84             mWindowGroup.CancelCaptureKeyUpAndDowns(handle);
       
    85         }
       
    86         mCapturedKeyUpDownHandles.clear();
       
    87 
       
    88         foreach (handle, mCapturedKeyHandles) {
       
    89             mWindowGroup.CancelCaptureKey(handle);
       
    90         }
       
    91         mCapturedKeyHandles.clear();
    73     }
    92     }
    74     foreach (handle, mCapturedKeyHandles) {
       
    75         mWindowGroup.CancelCaptureKey(handle);
       
    76     }
       
    77 
       
    78     CX_DEBUG_EXIT_FUNCTION();
    93     CX_DEBUG_EXIT_FUNCTION();
    79 }
    94 }
    80 
    95 
    81 bool CxuiCaptureKeyHandler::isAutofocusKeyPressed()
    96 bool CxuiCaptureKeyHandler::isAutofocusKeyPressed()
    82 {
    97 {
    93     CX_DEBUG_ENTER_FUNCTION();
   108     CX_DEBUG_ENTER_FUNCTION();
    94     bool eventWasConsumed = false;
   109     bool eventWasConsumed = false;
    95 
   110 
    96     if (event->type() == QEvent::KeyPress) {
   111     if (event->type() == QEvent::KeyPress) {
    97         QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
   112         QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
       
   113         CX_DEBUG(("CxuiCaptureKeyHandler - key press with nativeVirtualKey(): %d", keyEvent->nativeVirtualKey()));
       
   114         CX_DEBUG(("CxuiCaptureKeyHandler - key press with nativeScanCode(): %d", keyEvent->nativeScanCode()));
       
   115 
    98         if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode())
   116         if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode())
    99             && !mAutofocusKeyPressed ) {
   117             && !mAutofocusKeyPressed ) {
   100 
   118 
   101             mAutofocusKeyPressed = true;
   119             mAutofocusKeyPressed = true;
   102             eventWasConsumed = true;
   120             eventWasConsumed = true;
   108             eventWasConsumed = true;
   126             eventWasConsumed = true;
   109             emit captureKeyPressed();
   127             emit captureKeyPressed();
   110         }
   128         }
   111     } else if (event->type() == QEvent::KeyRelease) {
   129     } else if (event->type() == QEvent::KeyRelease) {
   112         QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
   130         QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
       
   131         CX_DEBUG(("CxuiCaptureKeyHandler - key press with nativeVirtualKey(): %d", keyEvent->nativeVirtualKey()));
       
   132         CX_DEBUG(("CxuiCaptureKeyHandler - key press with nativeScanCode(): %d", keyEvent->nativeScanCode()));
   113 
   133 
   114         if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode())
   134         if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode())
   115             && mAutofocusKeyPressed ) {
   135             && mAutofocusKeyPressed ) {
   116 
   136 
   117             mAutofocusKeyPressed = false;
   137             mAutofocusKeyPressed = false;
   127         }
   147         }
   128     }
   148     }
   129     CX_DEBUG_EXIT_FUNCTION();
   149     CX_DEBUG_EXIT_FUNCTION();
   130     return eventWasConsumed;
   150     return eventWasConsumed;
   131 }
   151 }
       
   152 
       
   153 /*!
       
   154 * Helper method to listen to given key (key code or scan code).
       
   155 * We need to listen to both "key up", "key down" and "key pressed" events to
       
   156 * get all the necessary events to handleKeyEvent(). If we e.g. just listen
       
   157 * to up/down events, the way native events are translated to QKeyEvents,
       
   158 * we only get QEvent::KeyRelease event when partially in background.
       
   159 * @param key Keycode or scancode for the key to listen. Both should be listened.
       
   160 */
       
   161 void CxuiCaptureKeyHandler::listenKey(int key)
       
   162 {
       
   163     // Capture key down and up events
       
   164     int handle = mWindowGroup.CaptureKeyUpAndDowns(key, 0, 0, CXUI_KEY_PRIORITY);
       
   165 
       
   166     // Handle < 0 means error.
       
   167     if (handle >= 0) {
       
   168         mCapturedKeyUpDownHandles.append(handle);
       
   169     } else {
       
   170         CX_DEBUG(("[WARNING] CxuiCaptureKeyHandler - Problem hooking to key-up/key-down with code: %d", key));
       
   171     }
       
   172 
       
   173     // Capture key press events
       
   174     handle = mWindowGroup.CaptureKey(key, 0, 0, CXUI_KEY_PRIORITY);
       
   175 
       
   176     if (handle >= 0) {
       
   177         mCapturedKeyHandles.append(handle);
       
   178     } else {
       
   179         CX_DEBUG(("[WARNING] CxuiCaptureKeyHandler - Problem hooking to key-press with code: %d", key));
       
   180     }
       
   181 }
       
   182 
       
   183 
       
   184 // end of file