Jump to content

Simulate holding down spacebar


Recommended Posts

Posted (edited)

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
Posted (edited)
11 minutes ago, Rays said:

Any particular reason as to why it should hold the key and not just press it?

It's about 100% faster in my case. Simulating a spacebar press 100x a second would also work, but thats not something what humans do.

Edited by MALS
Posted (edited)
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
Posted
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.

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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