Skip to content

Commit

Permalink
[Android] Simple haptic feedback
Browse files Browse the repository at this point in the history
HapticFeedbackConstants.KEYBOARD_TAP on all keypress and touchpad down
events triggered by QML.

Fixes #115
  • Loading branch information
Vogtinator committed Feb 4, 2025
1 parent b36f20d commit 0b87d79
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
30 changes: 30 additions & 0 deletions core/os/os-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,33 @@ char *android_basename(const char *path)

return nullptr;
}

void androidVibrate()
{
QAndroidJniEnvironment env;

// Call activity.getWindow().getDecorView().perforHapticFeedback(KEYBOARD_TAP);
QAndroidJniObject window = QtAndroid::androidActivity()
.callObjectMethod("getWindow", "()Landroid/view/Window;");

QAndroidJniObject decorView = window.callObjectMethod("getDecorView", "()Landroid/view/View;");

if (env->ExceptionCheck())
{
env->ExceptionDescribe();
env->ExceptionClear();
return;
}

bool success = decorView.callMethod<jboolean>("performHapticFeedback", "(I)Z", 3); // HapticFeedbackConstants.KEYBOARD_TAP

if (env->ExceptionCheck())
{
env->ExceptionDescribe();
env->ExceptionClear();
return;
}

if (!success)
qInfo() << "Haptic feedback failed";
}
2 changes: 2 additions & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ FILE *fopen_utf8(const char *filename, const char *mode);
#if defined(__ANDROID__)
/* Returns an allocated string or NULL on failure. */
char *android_basename(const char *path);
/* KEYBOARD_TAP vibration for the current View. */
void androidVibrate();
#endif

void *os_reserve(size_t size);
Expand Down
10 changes: 10 additions & 0 deletions qmlbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,23 @@ void QMLBridge::setButtonState(int id, bool state)
{
int col = id % KEYPAD_COLS, row = id / KEYPAD_COLS;

#ifdef Q_OS_ANDROID
if (state)
androidVibrate();
#endif

::keypad_set_key(row, col, state);
}

void QMLBridge::setTouchpadState(qreal x, qreal y, bool contact, bool down)
{
::touchpad_set_state(x, y, contact, down);

#ifdef Q_OS_ANDROID
if (down)
androidVibrate();
#endif

touchpadStateChanged();
}

Expand Down

0 comments on commit 0b87d79

Please sign in to comment.