Simulation keyboard events in minecraft with an auto-keyboard - c#

So I'm playing minecraft and I want to make a program that switches the tool on a set time frame, meaning it switches to the second item slot in the toolbar and then the third and so on. If you're curious I'm playing skyblock and have a cobblestone generator. It works perfectly in notepad and other "textboxes" like chrome with the timing and everything but it seems that minecraft does not allow simulated keyboard presses because it does not respond to the input given by the program. Even if i have the game in full screen (f11) it does not seem to work. Do you think the program itself is faulty or do I need to adapt it for minecraft specifically? Any tips at all would be very appreciated.
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
timer1.Start();
if (textBox1.Text == "")
{
int interval = 1; // set to 1 second for testing purposes. This is 200 in practise.
timer1.Interval = interval * 1000;
}
else
{
int interval = int.Parse(textBox1.Text);
timer1.Interval = 1000 * interval;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (i > 9)
{
this.Close();
}
i++;
keyboardPress(i);
}
private void keyboardPress(int i)
{
SendKeys.Send(Convert.ToString(i));
}

Related

How to move a winforms tool to the right and then to the left continuously in the forms by using point class and timer tools?

Purpose of this code was to move a title(label) first rightwards until it hits the 600th pixel on the X axis and then leftwards until it hits the 27th pixel on the X axis of the form by using 2 timer tools and the Point class. One timer for going right and the other timer for going left. They should've work by swithing on and off consecutively after one another, however it does not work.
The label is stuck at 600th X location and does not move back to where it was.
The timer interval is 100 so it moves with a decent speed that allows us to see it moving.
namespace AlanCevreHesabiUygulamasi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (label11Point.X == 27)
{
timer2.Stop();
timer1.Start();
}
if (label11Point.X == 599)
{
timer1.Stop();
timer2.Start();
}
}
Point label11Point = new Point(27, 32);
private void timer1_Tick(object sender, EventArgs e)
{
while (label11Point.X <= 600)
{
label12.Text = label11Point.X.ToString();
label11Point.X += 1;
label11.Location = label11Point;
break;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
while (label11Point.X >= 27)
{
label12.Text = label11Point.X.ToString();
label11Point.X -= 1;
label11.Location = label11Point;
break;
}
}
}
}
Label is stuck at 600th pixel of the form, does not move back. How to make it work?
I'm surprised you see movement resulting from a while loop in a timer tick handler. Why have a timer if your are going to do it that way.
In this solution, I have a timer, and the movements happen during a timer tick. I also have three possible directions, Right, Left and Stopped (in case you want to have a start/stop button).
In the Windows Forms designer, I dropped both a label and a timer on the form. I left their properties alone except for the timer's Interval property (that I set to 10 (ms))
Then I added an enum and an instance of the enum as a field to the Form class:
public partial class Form1 : Form
{
private enum Direction { MoveRight, MoveLeft, Stopped }
Direction _direction;
public Form1()
{
InitializeComponent();
}
}
I double-clicked the Caption area of the form in the designer to create a form Load handler, and added a call to start the timer:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
Finally, I double-clicked the timer to get a timer Tick handler and added some code:
private void timer1_Tick(object sender, EventArgs e)
{
var curLocation = label1.Location;
if (_direction == Direction.MoveRight && curLocation.X > 600)
{
_direction = Direction.MoveLeft;
}
else if (_direction == Direction.MoveLeft && curLocation.X < 27)
{
_direction = Direction.MoveRight;
}
int offset = _direction switch
{
Direction.MoveRight => 1,
Direction.MoveLeft => -1,
_ => 0,
};
curLocation.X += offset;
label1.Location = curLocation;
}
The _direction field determines if the label is moving to the right or the left.
How can you write the code without a timer?"
You asked "How can you write the code without a timer?" I'm still flabbergasted that your label moves as the result of a while loop in an event handler - something must have changed from my good-old understanding of Win32 processing.
Anyways, I cheat and await a call to Task.Delay instead of using a timer. Take my existing code and do the following:
Add two buttons to your form (One labeled Start (named StartBtn) and the other labeled Stop (named StopBtn).
Add another Direction-typed field to the class: Direction _previousDirection;
Comment out the call to timer1.Start(); in the Form1_Load handler
Comment out all the code in the timer1_Tick method (at this point, you could remove the timer from the form if you want)
Select both buttons (Start and Stop) and press <Enter>. This will bring up click handlers for both buttons.
Change the StopBtn button's handler to look like:
New Stop Button code:
private void StopBtn_Click(object sender, EventArgs e)
{
_previousDirection = _direction;
_direction = Direction.Stopped;
}
Change the StartBtn's handler to look like the following. Note that nearly everything in the while loop (except the call to Task.Delay) is the same as the previous timer tick handler code. Also note that I made the handler async void to allow for the await keyword to do it's magic.
Start Button code:
private async void StartBtn_Click(object sender, EventArgs e)
{
_direction = _previousDirection;
while (_direction != Direction.Stopped)
{
var curLocation = label1.Location;
if (_direction == Direction.MoveRight && curLocation.X > 600)
{
_direction = Direction.MoveLeft;
}
else if (_direction == Direction.MoveLeft && curLocation.X < 27)
{
_direction = Direction.MoveRight;
}
int offset = _direction switch
{
Direction.MoveRight => 1,
Direction.MoveLeft => -1,
_ => 0,
};
curLocation.X += offset;
label1.Location = curLocation;
await Task.Delay(10);
}
}
I solved it, I don't know why but in my opening post the Form1() function only makes one of the if() conditions work. However, putting the if() statements into the timers solved the problem. Now, the title goes back and forth in the specified x axis intervals.
namespace AlanCevreHesabiUygulamasi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Start();
}
Point label11Point = new Point(27, 32);
private void timer1_Tick(object sender, EventArgs e)
{
while (label11Point.X >= 27)
{
label12.Text = label11Point.X.ToString();
label11Point.X += 1;
label11.Location = label11Point;
break;
}
if (label11Point.X == 600)
{
timer2.Start();
timer1.Stop();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
while (label11Point.X <= 600)
{
label12.Text = label11Point.X.ToString();
label11Point.X -= 1;
label11.Location = label11Point;
break;
}
if (label11Point.X == 27)
{
timer1.Start();
timer2.Stop();
}
}

Playing a Sound for a pre specified time using timer in .Net Forms C#

I am having a problem in .Net Windows Forms. Where I need to click an image and play a sound for a certain duration defined by a variable ("im.duration"). I have a timer as well with the tick Eventhandler attached. By the following code below the sound plays repeatedly without stopping.
How is it possible to make any adjustments to make the sound play for a specified duration?
private void Image_Click(object sender, EventArgs e)
{
foreach (Image im in panel2.Controls.OfType<Image>())
{
if (sender == im)
{
timer1.Enabled = true;
count = 0;
timer1.Start();
string fileToPlay = Environment.CurrentDirectory + #"\ImageSound\" + im.sound.ToString() + ".wav";
SoundPlayer sp = new SoundPlayer(fileToPlay);
while (count <= im.Duration)
{
sp.Play();
}
timer1.Enabled = false;
sp.Stop();
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
count++;
}
Assuming Duration is the number of milliseconds to play the sound file, you could do:
private async void Image_Click(object sender, EventArgs e)
{
Image im = (Image)sender;
string fileToPlay = System.IO.Path.Combine(Environment.CurrentDirectory, "ImageSound", im.sound.ToString() + ".wav");
SoundPlayer sp = new SoundPlayer(fileToPlay);
sp.Play();
await Task.Delay(im.Duration);
sp.Stop();
}
If Duration is the number seconds, then change im.Duration to TimeSpan.FromSeconds(im.Duration).
In my testing, this successfully cut off an 8 second .wav file at whatever shorter duration I specified.
Note that we don't need the for loop at all. We simply cast sender to class Image.
By the way, Image is a horrible name for your class since that is already a built-in class in .Net: Image

Progressbar stops at +-90%

I am making a Q&A program where you have 15 sec to answer a question. The problem is that the progress bar is only filling like 90% and moves to the next question. I can put the code here but I have worked in Dutch so maybe it's difficult to understand.
private void volgendeVraagFormButton_Click(object sender, EventArgs e)
{
//button for going to next question
vraagFormulierProgressBar.Value = 0;
vraagFormulierTimer.Start();
}
private void vraagFormulierTimer_Tick(object sender, EventArgs e)
{
if (vraagFormulierProgressBar.Value < vraagFormulierProgressBar.Maximum)
vraagFormulierProgressBar.PerformStep();
//checks if the value is lower than 15 sec(max)
else
{ //stops the progress if the 15 secs are over and moves to next question
vraagFormulierTimer.Stop();
vraagFormulierProgressBar.Value = 0;
vraagFormulierTimer.Start();
}
}
Here's the same code with variable names in English:
private void nextQuestionFormButton_Click(object sender, EventArgs e)
{
//button for going to next question
questionFormProgressBar.Value = 0;
questionFormTimer.Start();
}
private void questionFormTimer_Tick(object sender, EventArgs e)
{
if (questionFormProgressBar.Value <questionFormProgressBar.Maximum)
questionFormProgressBar.PerformStep();
//checks if the value is lower than 15 sec(max)
else
{ //stops the progress if the 15 secs are over and moves to next question
questionFormTimer.Stop();
questionFormProgressBar.Value = 0;
questionFormTimer.Start();
}
}
Take a look at this: Disabling .NET progressbar animation when changing value?
So try to increment your progress bar by 2, then decrement by 1. That should fix the animation problem
Edit
Also, change this line
if (vraagFormulierProgressBar.Value < vraagFormulierProgressBar.Maximum)
to this
if (vraagFormulierProgressBar.Value + 1 < vraagFormulierProgressBar.Maximum)
Edit 2
OK, I've got it this time. First, set the maximum of your progress bar to 300 and the interval to 1 (You can fix the timing later). Next, replace the timer tick function with this:
if (progressBar1.Value < progressBar1.Maximum - 1)
{
progressBar1.Increment(2);
progressBar1.Increment(-1);
}
else
{
timer1.Stop();
progressBar1.Maximum = 10000;
progressBar1.Value = 10000;
progressBar1.Value = 9999;
progressBar1.Value = 10000;
System.Threading.Thread.Sleep(150);
progressBar1.Value = 0;
progressBar1.Maximum = 300;
timer1.Start();
}
Sorry about using the English names, I copied this out of my test form. Anyway, hope this helps!

c# stop button for beep sound

I created a simple C# app that uses the beep console. I want to add a stop button to stop the beeping, but once the app starts to run it doesnt let me hit a close/button button. Below is the code i have.
private void button1_Click(object sender, EventArgs e)
{
int int1, int2, hours;
int1 = int.Parse(txtbox1.Text);
int2 = int.Parse(txtbox2.Text);
hours = ((60 / int1) * int2);
for (int i = 0; i <= hours; i++)
{
Console.Beep();
Thread.Sleep(int1 * 60000);
}
}
The reason is that you execute button1_Click in the GUI thread. When you call this method the thread will be stuck there for quite some time because you make it sleep.
If you remove Thread.Sleep(int1*60000); you will notice that your application is unresponsive until it is done beeping.
You should try to use a Timer instead. Something like this should work (this is based on the Windows.Forms.Timer):
private Timer timer = new Timer();
And set it up
timer.Tick += OnTick;
timer.Interval = int1 * 60000;
...
private void OnTick(object o, EventArgs e)
{
Console.Beep();
}
In your buttonclick you are now able to start and stop the timer:
timer.Start();
or
timer.Stop();
I would use a timer in this case, the reason why you can't close the form is because you are calling a sleep on the form thread from what I understand. Calling a sleep on the form thread will give the impression the app has crashed.
Here is a quick sample code I built in c#, it will beep the console at the time given. I hope it helps.
private void button1_Click(object sender, EventArgs e)
{
timer1.Tick += new EventHandler(timer_dosomething);
timer1.Interval = 60000;
timer1.Enabled = true;
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
void timer_dosomething(object sender, EventArgs e)
{
Console.Beep();
}

Playing a different segments in a video

i want to develop a video player which has to play different parts in a video (specified by their start n end positions). I am using directx.audiovideoplayback dll for this purpose.
the starting and ending positions are stored in an array.
eg- {2,6,8,19} tells that segment between 2nd to 6th second has to be played and then 8th to 19th second should be played.
my problem is that despite me giving condition that
if(video_obj.CurrentPosition==endtime) video_obj.Stop();
the video isnt stopping..
the video is playing from 2nd position to end of file.
code is
public static int[] seconds = { 3,8,12,16};
public static int start, end;
private void btnPlay_Click(object sender, EventArgs e)
{
vdo.Owner = panel1;
panel1.Width = 300;
panel1.Height = 150;
vdoTrackBar.Minimum = 0;
vdoTrackBar.Maximum = Convert.ToInt32(vdo.Duration);
if (vdo != null)
{
vdo.Play();
timer1.Start();
}
}
private void vdoTrackBar_Scroll(object sender, EventArgs e)
{
if (vdo != null)
{
vdo.CurrentPosition = vdoTrackBar.Value;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int i;
for (i = 0; i < seconds.Length; i = i + 2)
{
start = seconds[i];
vdo.CurrentPosition = start;
end = seconds[i + 1];
vdoTrackBar.Value = (int)vdo.CurrentPosition;
MessageBox.Show("Starts at " + vdo.CurrentPosition + " and Ends at " + end);
if (vdo.Paused)
vdo.Play();
if (vdo.Playing)
{
if (vdo.CurrentPosition == vdo.Duration)
{
timer1.Stop();
vdo.Pause();
vdoTrackBar.Value = 0;
}
if (vdo.CurrentPosition == end)
{
timer1.Stop();
vdo.Pause();
}
vdoTrackBar.Value += 1;
}
}
Help! Somewhere something is wrong and i have no clue about it
How do i correct it?
Video starts playing when i
So there still isn't enough info in the post to really definitively say whats going wrong. I'm guessing that your timer isn't ticking with enough resolution to happen to tick exactly when the for loop in the timer tick would coincide with the video's current position.
How often does the timer tick? What's the precision of the video's current position?

Categories