Search code examples
androidadb

How to generate Ctrl+v(Paste) like similar event from an android app?


I am trying to generate a Paste event in android. I have already read about "Keyevents" in android but till yet I have not found any key to generate paste event. This stackoverflow question is also not helpful.

Even the accepted answer is not very clear. So is there any way to do this like by using any 3rd party library, etc. Please help


Solution

  • So finally after a lot of research I have found one way to paste the copied text and that is by using Accessibility Service.

    I got the solution while searching for one exception from this stackoverflow thread.

    Still for a quick view I am pasting the code over here too, If you already have copied the text and your Accessibility Service is all set then you can just use this piece of code:

      @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
    
            AccessibilityNodeInfo source = event.getSource();
            if (source != null && ( event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED ) ) {
    
                    try{
                            Thread.sleep(2000);
                        }catch (Exception e){
    
                        }
                source.performAction(AccessibilityNodeInfo.ACTION_PASTE); 
    
            }
        }   
    

    I have added Thread.sleep(2000) to give sometime for UI to load and then paste text later.