Jump to content

Simulate holding down spacebar


MALS

Recommended Posts

Hey,

I am writing a script that has to do a lot of talking with NPC's. In order to speed this up I want to simulate holding down the spacebar, but I can't seem to get it working.

The build in Dialogues class does press spacebar to continue, but doesn't hold it down.

The Keyboard class has a pressKey() method but this also doesn't seem to hold down spacebar.

My question; is it possible to simulate holding down the spacebar in dialogues?

Thanks in advance!

 

edit:

Found a solution myself!

public void holdSpacebar() throws InterruptedException {
	while (getDialogues().isPendingContinuation()) {
		getKeyboard().pressKey(' ');
		sleep(30);
	}
	getKeyboard().releaseKey(' ');
}

By testing I found that a key gets repeated about 2000 times a minute on Windows 10 with the repeat rate set to max in keyboard properties. This translates to a key press every 30ms.

This method seems to have the same behavior as a human holding down spacebar. The talking process in my script seems to be sped up by about 100%!

Edited by MALS
Link to comment
Share on other sites

1 hour ago, MALS said:

Nope, this would press the spacebar repeatedly and not hold it :(

 

You should have a look at the conditional sleep class

 

  if (getDialogues().isPendingContinuation()) {

   //hold space
   getKeyboard().pressKey(KeyEvent.VK_SPACE);

   int timeout = 1600; //2 tick surpassed + 400ms ping compensate
   int sleepTime = 700; //1 tick surpassed + 100ms ping compensate

   //sleep until isPendingContinuation() == false
   new ConditionalSleep(timeout, sleepTime) {
    @Override
    public boolean condition() throws InterruptedException {
     return !getDialogues().isPendingContinuation();
    }
   }.sleep();

   //release space
   getKeyboard().releaseKey(KeyEvent.VK_SPACE);

  }

 

Edited by Zappster
Link to comment
Share on other sites

8 hours ago, Zappster said:

 

You should have a look at the conditional sleep class

 


  if (getDialogues().isPendingContinuation()) {

   //hold space
   getKeyboard().pressKey(KeyEvent.VK_SPACE);

   int timeout = 1600; //2 tick surpassed + 400ms ping compensate
   int sleepTime = 700; //1 tick surpassed + 100ms ping compensate

   //sleep until isPendingContinuation() == false
   new ConditionalSleep(timeout, sleepTime) {
    @Override
    public boolean condition() throws InterruptedException {
     return !getDialogues().isPendingContinuation();
    }
   }.sleep();

   //release space
   getKeyboard().releaseKey(KeyEvent.VK_SPACE);

  }

 

Yeah you'd think this would work but in the problem is that getKeyboard().pressKey(KeyEvent.VK_SPACE); doesn't actually seems to hold the spacebarm, but only press it once in a dialogue. Overnight I might have came up with a solution though, if you long press a key in the chatbox, it types like 10 characters a second, the same as calling typeKey(' ') 10 times a second would do. I might just simulate that.

Link to comment
Share on other sites

  • 3 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...