33

My project is currently using but seems I have moved to api level 26 - revision 26.0.2, I am struggling to find the symbol KeyEventCompat

import android.support.v4.view.KeyEventCompat;

I try to figure out using v7 but it's not working. Any idea on how to make it works.

Example of use:

case KeyEvent.KEYCODE_TAB:
    if (KeyEventCompat.hasNoModifiers(event)) {
        handled = arrowScroll(FOCUS_FORWARD);
    } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
        handled = arrowScroll(FOCUS_BACKWARD);
    }
    break;

Any idea on how to make it works.

Thanks

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Seb
  • 2,929
  • 4
  • 30
  • 73
  • [The class has been deprecated](https://github.com/bumptech/glide/issues/2707)... what exactly are you trying to do with that class? – Chris Stillwell Apr 30 '18 at 20:39
  • catching event. the project is done since a while but we are updating it – Seb Apr 30 '18 at 20:42
  • What event are you trying to catch? Can you post a code snippet on what you are trying to do? I think you'll get better help asking people how you can replace the deprecated class than keep using it. – Chris Stillwell Apr 30 '18 at 20:45
  • done. I have added a code snippet – Seb Apr 30 '18 at 20:50
  • What API's are you supporting? I did some more reading and it looks like KeyEventCompat was deprecated because KeyEvent supports the minimum support library (API 14). So, if you support anything lower than API 14, you'll probably run into other issues with the support libraries. The `hasNoModifiers` call requires API 11+, btw. – Chris Stillwell Apr 30 '18 at 20:56
  • minimum supported is 19 and the target is 26 – Seb Apr 30 '18 at 20:59
  • @sed did you get any solution ? – Tushar Lathiya Jun 19 '18 at 13:47
  • @TusharLathiya You can check my answer – Madhur Jun 27 '18 at 03:35

3 Answers3

89

change this

if (KeyEventCompat.hasNoModifiers(event)) {
    handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
    handled = arrowScroll(FOCUS_BACKWARD);
}

to

if (event.hasNoModifiers()) {
    handled = arrowScroll(FOCUS_FORWARD);
} else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
    handled = arrowScroll(FOCUS_BACKWARD);
}

KeyEventCompat class was deprecated in API level 26.0.0

Madhur
  • 3,303
  • 20
  • 29
0

Since you are supporting API 19+, change your KeyEventCompat calls to KeyEvent calls. You should be able to get comparable functionality at that API level.

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
0

I resolved this KeyEventCompat issue by adding this line to the app's build gradle (Above dependencies)

configurations.all {
    exclude group: 'com.google.code.gson'

    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.0.2'
            }
        }
    }
}
itzo
  • 1,220
  • 14
  • 18