Solved

Override android volume keys

Posted on 2014-03-21
4
2,169 Views
Last Modified: 2014-03-21
Good day, i've got a mission to do.

First, we had an Android application developed for devices with TRACKPAD (yes, that square that several blackberrys have).

Now, we only have devices that doesn't have this key on. This way the user is FORCED to touch the screen.

Im searching for alternatives to navigate the device WITHOUT touching the screen.

I wonder what were those guys thinking when offering a hardware keyboard with no trackpad or way to move around the phone but touching the screen.....

Here it is what i've done:

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
	    if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
	        if (event.getAction() == KeyEvent.ACTION_DOWN){


	        	super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_UP));
	        	super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_DPAD_UP));
	        	
	            return true;
	    }}
	    
	    if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
	        if (event.getAction() == KeyEvent.ACTION_DOWN){

	        	super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_DOWN));
	        	super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_DPAD_DOWN));
	        	
	            return true;
	    }}
	    return super.dispatchKeyEvent(event);
	};

Open in new window


This works PERFECT for overriding the volume keys on this activity.
Also, i am trying to fire the KeyEvent.KEYCODE_DPAD_DOWN event and i have NO SUCCESS. What am i missing??

In other words, what i am trying to do is to execute the KEYCODE_DPAD_DOWN  when pressed the volume key, with this code i posted i am trying to do so however, i only get the overriden volume key and STILL need the execute of the KEYCODE_DPAD_DOWN :/
0
Comment
Question by:JavierVera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:btdownloads7
ID: 39946871
You're approaching the interception of volume keys a bit wrong. You just need to override the key listener in the activity, and make it listen for volume keys. I'm also not sure that you'll be able to manually dispatch the DPAD key events, nor do you really need to -- just call whatever function is supposed to be called when those events would be triggered.

@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
			//call whatever the function was supposed to be for DPAD_UP and consume the event
	        return true;
		}else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
			//call whatever the function was supposed to be for DPAD_DOWN and consume the event
	        return true;
		}else{
			// pass the key event onto the OS
			return super.onKeyDown(keyCode, event);
		}
	}

Open in new window

1
 
LVL 1

Author Comment

by:JavierVera
ID: 39946921
Thing i want to emulate is the focus change with the DPAD.
I have a layout with 3 textboxes. I can easily move around them with the TRACKPAD.
I want the same functionality with the volume keys. UP and DOWN that's it.

This is why i want to intercept the event and change it to this purpose. If you have any aproach like this please tell.

There is no TabIndex in Android as for use it as event as you state on the snippet you are showing.

I've tryed the following with no success....

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		int keyCode = event.getKeyCode();
		boolean result = false;
		
		if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
			super.onKeyDown(KeyEvent.KEYCODE_DPAD_UP, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP));
			
			return true;
		} else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
			super.onKeyDown(KeyEvent.KEYCODE_DPAD_DOWN, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
			
			return true;
		}
		
		return false;
		//return super.dispatchKeyEvent(event);
	}

Open in new window


and

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean result = false;
		// Detectar y manejar el uso del botón HOME
		if (keyCode == KeyEvent.KEYCODE_HOME) {
			setQuitStartTime();

			Intent startMain = new Intent(Intent.ACTION_MAIN);
			startMain.addCategory(Intent.CATEGORY_HOME);
			startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(startMain);
			result = true;
		}
		if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
			super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP));
			
			result = true;
		} else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
			super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
			
			result = true;
		}
		else {
			result = super.onKeyDown(keyCode, event);
		}
		return result;
	}

Open in new window



any idea please ?
0
 
LVL 14

Accepted Solution

by:
btdownloads7 earned 500 total points
ID: 39946941
Your 2nd approach to intercepting the volume keys is correct (as I posted in my original response), but as I also said there, you may not want to generate the DPAD events, but instead just launch whatever functionality the DPAD events are mapped to. So, for example, when you intercept a volume down keypress, just programmatically select the next text field.

But if you do insist on generating a DPAD key event (which I'm addmittedly not an expert at), you probably should generate both the ATCTION_DOWN event and then the ACTION_UP event (because that's how it works with physical key presses).

And I saw a different way of generating the DPAD actions on stackoverflow, but I don't know if it works:

BaseInputConnection bic=new BaseInputConnection(this.getWindow().getDecorView(),false);
KeyEvent event2 = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN,0, KeyEvent.META_SYM_ON, 0, 0, KeyEvent.FLAG_VIRTUAL_HARD_KEY); 

bic.sendKeyEvent(event2);

Open in new window

0
 
LVL 1

Author Closing Comment

by:JavierVera
ID: 39946952
This approach with the BaseInputConnection is what i really wanted. Thank you very much.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question