Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 48
Registered: ‎06-02-2013
My Device: 9780
My Carrier: MTS UKR

Re: Invoking PASTE menu action from a background thread

I guess, with the help of ApplicationMenuItemRepository addMenuItem() on the first run of the app. Sure thing, checking afterwards in order not to create a duplicate menu item. Or did you ask about the exact coding to process this? Tomorrow I will try this approach and hope it will be what I need. Will keep posted.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Invoking PASTE menu action from a background thread

I think I would rather check that paste was really an option in the Field I am currently in.  And I think having an extra menu in every application is not very user friendly.  So I would still try to find the Paste menu option. 

Highlighted
Developer
Posts: 48
Registered: ‎06-02-2013
My Device: 9780
My Carrier: MTS UKR

Re: Invoking PASTE menu action from a background thread

[ Edited ]
if (field == buttonField) {
			try {
				Thread t = new Thread(new Runnable(){
					public void run() {
						synchronized (UiApplication.getUiApplication().getActiveScreen()) {
							Clipboard cb = Clipboard.getClipboard();
							cb.put("1");
							pressPasteMenu();
						}
					}
				});
				
				UiApplication.getUiApplication().requestBackground();
				
				synchronized (this) {
					wait(200);
				}
				t.start();
			} catch (Exception e) {
			}
		}
	}

	private void pressPasteMenu() {
		Ui.getUiEngine().getActiveScreen().getFieldWithFocus().paste(Clipboard.getClipboard());
		Menu menu = Ui.getUiEngine().getActiveScreen().getMenu(0);
		for (int i = 0; i < menu.getSize(); i++) {
			if (menu.getItem(i).toString().equalsIgnoreCase("Paste")) {
				menu.getItem(i).run();
			}
		}
	}

 The above code seems to be able to run the Paste menu item. However, it simply does not. What am I missing about it? All the permissions for the app are "Allow".

 

And by the way, does anyone know the reason why the code from the first message of this topic works fine in OS 5, while in OS 6-7 the injection makes extra navigation key press after injecting the clipboard object? This is a mystery to me...

Developer
Posts: 48
Registered: ‎06-02-2013
My Device: 9780
My Carrier: MTS UKR

Re: Invoking PASTE menu action from a background thread

Generally, I managed to cope with what I needed, though using navigation click injection.

But stil I would like to know why the above code in the part considering menu item running would not work. Can you point out my mistake in pressPasteManu() ?

Would be very grateful to anyone.

Developer
Posts: 48
Registered: ‎06-02-2013
My Device: 9780
My Carrier: MTS UKR

Re: Invoking PASTE menu action from a background thread

Ui.getUiEngine().getActiveScreen().getFieldWithFocus();
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, (char) Keypad.KEY_MENU, KeypadListener.STATUS_NOT_FROM_KEYPAD));
EventInjector.invokeEvent(new EventInjector.NavigationEvent(EventInjector.NavigationEvent.NAVIGATION_MOVEMENT , 0, -1, KeypadListener.STATUS_FOUR_WAY));

 This snippet gives me direct access to Paste menu item when the symbol from the clipboard is to be inserted into an empty field (-1 is the exact movement needed to this). Yet, when the field is pre-populated with some more symbols and the one I need is to be inserted in the middle of them, -1 is not the correct value to move for, as Paste moves higher in the menu... I am a little bit stuck.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Invoking PASTE menu action from a background thread

I don't think there is a bullet proof way of doing what you are trying to do. 

 

But if you can find the Field, you can request that Field's context menu, and if you can do that, you search the context menu for Paste. And if you find the Paste menu item, your can 'run' it.  As noted previously, I would use that in preference to Key Injection. 

Developer
Posts: 48
Registered: ‎06-02-2013
My Device: 9780
My Carrier: MTS UKR

Re: Invoking PASTE menu action from a background thread

The matter is that I don't "own" the field I am trying to paste to. It doed not belong to my app, but to any app systemwide. And I don't seem to be getting the context menu of the field... Or am I doing smth wrong?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Invoking PASTE menu action from a background thread

[ Edited ]

Sorry I am not speaking from a position of great strength here because I have never done this, just looked at the theory of doing it.  I don't have time atm to actually try it myself, which is a shame.  So take all this with a huge grain of salt, it might all be porky pies. 

 

I thought if you are running an ApplicationManuItem, then you are not running in your Application context, but in the Application context of the running Application.  So while the Field you will find will not belong to your Application, that is not in fact important, since you are running in the Application that owns the Screen/Field anyway.

 

Can you try debugging and seeing if you actually get the Field you want. 

 

You say this:"don't seem to be getting the context menu of the field".  What do you get?

 

Developer
Posts: 48
Registered: ‎06-02-2013
My Device: 9780
My Carrier: MTS UKR

Re: Invoking PASTE menu action from a background thread

Well, I must have partially misled you about the ApplicationMenuItem... For now, I haven't created one I was speaking about earlier. I am trying to avoid it. Just looking for a nice solution.

If I created my own "paste" menu item, then everything you say would be true, I guess. But what i am trying to do is to use the context paste menu item. And no luck yet.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Invoking PASTE menu action from a background thread

"For now, I haven't created one"

 

Ah, perhaps that is part of the problem.

 

getActiveScreen(), from my experience, will not find the screen on the top - it will find the top most screen of the Application in which it is run. So if you do this from your Application context, you will get your top most screen.  Even if your application is running in the background, this will still see your screen.  This is because of the way the OS sandboxes all the Applications - you don't see things that belong to other applications.