0

I have sort of found a solution to the question My previous question and it was to use:

SendKeys.Send("{ENTER}"); to automatically click OK button on a dialog box

My problem now is that SendKeys.Send("{ENTER}"); will work in an event method i.e Start_click but will not in the method Start_Vid();

I get the error:

'SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method'

I have no idea why it should not work and what the error message means?

The code is below:

using System;
using System.Threading;
using System.Windows.Input;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using Accord.Video.FFMPEG;
using AForge.Video.VFW;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection VideoCaptureDevices;
        private VideoCaptureDevice FinalVideo = null;
        private VideoCaptureDeviceForm captureDevice;
        private Bitmap video;
        private VideoFileWriter FileWriter = new VideoFileWriter();
        private SaveFileDialog saveAvi;

        public Form1()
        {
            InitializeComponent();
            Console.WriteLine(date1);
            Console.WriteLine(date2);
            Console.WriteLine(date3);
            Console.WriteLine(date3);

            Start_Vid();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            captureDevice = new VideoCaptureDeviceForm();

        }

        void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            if (Stop.Text == "Stop Record")
            {
                video = (Bitmap)eventArgs.Frame.Clone();
                pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
                //AVIwriter.Quality = 0;
                FileWriter.WriteVideoFrame(video);
                //AVIwriter.AddFrame(video);
            }
            else
            {
                video = (Bitmap)eventArgs.Frame.Clone();
                pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
            }
        }

        private void Stop_Vid()
        {
            if (Stop.Text == "Stop Record")
            {
                Stop.Text = "Stop";
                if (FinalVideo == null)
                { return; }
                if (FinalVideo.IsRunning)
                {
                    //this.FinalVideo.Stop();
                    FileWriter.Close();
                    //this.AVIwriter.Close();
                    pictureBox1.Image = null;
                }
            }
            else
            {
                this.FinalVideo.Stop();
                FileWriter.Close();
                //this.AVIwriter.Close();
                pictureBox1.Image = null;
            }
        }
        private void butstop_Click(object sender, EventArgs e)
        {
            if (Stop.Text == "Stop Record")
            {
                Stop.Text = "Stop";
                if (FinalVideo == null)
                { return; }
                if (FinalVideo.IsRunning)
                {
                    //this.FinalVideo.Stop();
                    FileWriter.Close();
                    //this.AVIwriter.Close();
                    pictureBox1.Image = null;
                }
            }
            else
            {
                this.FinalVideo.Stop();
                FileWriter.Close();
                //this.AVIwriter.Close();
                pictureBox1.Image = null;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            pictureBox1.Image.Save("IMG" + DateTime.Now.ToString("hhmmss") + ".jpg");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FinalVideo == null)
            { return; }
            if (FinalVideo.IsRunning)
            {
                this.FinalVideo.Stop();
                FileWriter.Close();
                //this.AVIwriter.Close();
            }
        }

        private void Save_Click(object sender, EventArgs e)
        {
            saveAvi = new SaveFileDialog();
            saveAvi.Filter = "Avi Files (*.avi)|*.avi";
            saveAvi.FileName = "New Vid1";

            if (saveAvi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
                int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;
                FileWriter.Open(saveAvi.FileName, w, h, 25, VideoCodec.Default, 5000000);
                FileWriter.WriteVideoFrame(video);


                //AVIwriter.Open(saveAvi.FileName, w, h);
                Stop.Text = "Stop Record";
                //FinalVideo = captureDevice.VideoDevice;
                //FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
                //FinalVideo.Start();
            }


        }

        //##############################################//
        //        Method not working        //
        //##############################################//

        private void Start_Vid()
        {
            SendKeys.Send("{ENTER}");
            if (captureDevice.ShowDialog(this) == DialogResult.OK)
            {

                VideoCaptureDevice videoSource = captureDevice.VideoDevice;
                FinalVideo = captureDevice.VideoDevice;
                FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
                FinalVideo.Start();
            }
        }

        //##############################################//
        //        Method working            //
        //##############################################//

        private void Start_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{ENTER}");
            if (captureDevice.ShowDialog(this) == DialogResult.OK)
            {
                VideoCaptureDevice videoSource = captureDevice.VideoDevice;
                FinalVideo = captureDevice.VideoDevice;
                FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
                FinalVideo.Start();
            }
        }
        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
Kenny Barber
  • 245
  • 2
  • 11
  • What if you move the `Start_Vid` call to `Form1_Load`? – zneak Aug 25 '18 at 14:24
  • Hi zneak, I tried this and it does run the program but it doesn't automatically press enter like it does with Start_Click. – Kenny Barber Aug 25 '18 at 14:53
  • 1
    To my opinion you are on the wrong track. I read also your previous question but it is not clear if you have/do not have access to this ok button. – Aldert Aug 25 '18 at 15:41
  • You want to show the form and close it immediatelly(with the *ok* button) so to get the parameters for the video source. It **doesn't** make any sense! Just get them without opening the dialog. – γηράσκω δ' αεί πολλά διδασκόμε Aug 25 '18 at 15:59
  • Hi Zneak, for whatever reason Start_Vid() can be called from Form1_Load. But Start_Vid() keeps on looping and constantly pressing ENTER. I need a way of running Start_Vid() once and sit and wait for the next command. Is there any way to do this? – Kenny Barber Aug 26 '18 at 09:26

1 Answers1

0

Try using the SendKeys.SendWait method.
If it doesn't work try this AutoHotKey wrapper from andrew.

And by using these lines of code should do the trick.

var ahk = AutoHotkeyEngine.Instance;
ahk.ExecRaw("Send {Enter}");
  • Hi T0bi, thanks for your answer. I did manage to get SendKeys.Send("{ENTER}") working with the Start_Vid() method, the only problem is that it keeps on repeating the ENTER press when I want this to do it once. Do you have any ideas on this please? Also I tried the autohotkey wrapper but when trying to add reference the autohotkey.dll it gives an error saying it is can't be added as its not accessible, that it is a valid assembly or COM component. – Kenny Barber Aug 26 '18 at 12:20
  • you should build it first or download the built one from https://github.com/T0bi-Ethirbirge/1024572950/blob/master/AutoHotKeyWrapper.dll – T0bi Ethirbige Aug 26 '18 at 12:31