Creating custom pop up keyboard nothing happen when keys are press - c#

What I want to happen is that when a textbox is clicked a pop up keyboard will show and whatever i type on the keyboard will be putt in the textbox, but nothing happen but when I use notepad, it works. How can I fix this?
Here is my main form code:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
Form1 frm1 = new Form1();
frm1.ShowDialog();
}
And here is my code for pop up keyboard
public partial class Form1 : Form
{
const int WS_EX_NOACTIVATE = 0x08000000;
//this line of code fixed the issue
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
public Form1()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ExStyle |= WS_EX_NOACTIVATE;
//This line of code fix the issues
param.Style = 0x40000000 | 0x4000000;
param.Parent = GetDesktopWindow();
return param;
}
}
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("1");
}
private void button3_Click(object sender, EventArgs e)
{
SendKeys.Send("2");
}
private void button4_Click(object sender, EventArgs e)
{
SendKeys.Send("3");
}
private void button2_Click(object sender, EventArgs e)
{
}
}

Use form1.Show() ... not ShowDialog()

If you manually added the testbox make sure you hook into the MouseEventHandler like so:
this.textBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseClick);
when your form loads.

I fixed the issue by just adding this line of code
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
And
param.Style = 0x40000000 | 0x4000000;
param.Parent = GetDesktopWindow();

Related

What is the right way to execute c# code in a powershell script?

I'm trying to play/pause media and I found some c# code but when I try to implement it into my powershell script, nothing happens... This is what I tried:
Add-Type #'
using System;
using System.Windows;
using System.Runtime.InteropServices;
namespace UniversalSandbox
{
public partial class Form1
{
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
private void button1_Click(object sender, EventArgs e)
{
keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
}
private void button2_Click(object sender, EventArgs e)
{
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
}
private void button3_Click(object sender, EventArgs e)
{
keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
}
}
}
'#
[UniversalSandbox.Form1]::button1_Click;
I call the play/pause event using [UniversalSandbox.Form1]::button1_Click; but nothing happens... Is my syntax wrong?
(I found the c# code here)
You example is a Windows Forms app. You need to add some extra buttons and create form instance (GUI) and click buttons to manipulate it. button1_click is event you need to assign to the button control (you can't call it directly). Below is updated example.
Add-Type #'
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
namespace UniversalSandbox
{
public partial class Form1 : Form
{
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void InitializeComponent(){
var button1 = new Button();
var button2 = new Button();
var button3 = new Button();
button1.Text = "Play";
button1.Location = new Point(20, 100);
button1.Click += new EventHandler(button1_Click);
button2.Text = "Prev";
button2.Location = new Point(110, 100);
button2.Click += new EventHandler(button2_Click);
button3.Text = "Next";
button3.Location = new Point(200, 100);
button3.Click += new EventHandler(button3_Click);
this.Controls.Add(button1);
this.Controls.Add(button2);
this.Controls.Add(button3);
}
private void button1_Click(object sender, EventArgs e)
{
keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
}
private void button2_Click(object sender, EventArgs e)
{
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
}
private void button3_Click(object sender, EventArgs e)
{
keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
}
}
}
'# -ReferencedAssemblies System.Windows.Forms, System.Drawing
$player = [UniversalSandbox.Form1]::new()
$player.ShowDialog()
Mike Twc's answer shows an effective solution that adds many pieces that were missing from your code.
As for what you tried in terms of syntax:
[UniversalSandbox.Form1]::button1_Click
tries to access a static (::) public member named button1_Click.
If there were such a method (there isn't, because (a) you've made it an instance method and (b) you've made it private), accessing it without parentheses () (including required arguments) would print the method's signature (its return type, name, parameter names and types) rather than call it.

C# Prevent Blinking Cursor in Textbox, Solution not Working

I have found many pages describing how to prevent the blinking caret. Looks simple enough.
[DllImport("user32")]
public static extern bool HideCaret(IntPtr hWnd);
private void OnFocusEnterSpecificTextbox(object sender, EventArgs e)
{ HideCaret(SpecificTextbox.Handle); }
It's not working. When I click on the Textbox, there's the caret. I can breakpoint and see that I am hitting that code.
What boneheaded mistake am I making?
This works (VS 2008 on Windows 7):
public partial class Form1 : Form
{
[DllImport("user32")]
public static extern bool HideCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
HideCaret(textBox1.Handle);
}
}
Here's another way to stop blinking cursor in TextBox:
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
textBox1.GotFocus += (s1, e1) => { HideCaret(textBox1.Handle); };
}
}

Sendkeys event is not detected inside the application in windows form application

I am new to windows app development. I am developing a Windows Form Application where the layout is as follows:
There is one textbox and i have created the keyboard inside the application using SendKeys event.
Problem is that all other application on the system are able to detect the keys but the textbox inside the application is not able to detect the keys.
Basically app is having complete keyboard this is just one button press code
What I have tried:
public partial class Form1 : Form
{
Control focusedC;
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ExStyle |= 0x08000000;
return param;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape) {
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
TopMost = false;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//checkbox is for CapsLock Key
}
private void button14_Click(object sender, EventArgs e)
{
if (checkBox1.Checked && focusedC != null)
{
focusedC.Focus();
SendKeys.Send("Q");
}
else if(focusedC != null)
{
focusedC.Focus();
SendKeys.Send("q");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
focusedC = sender as TextBox;
}
}
Of cource it doesn't work on your window. You set the WS_EX_NOACTIVATE style! It works on other windows but not yours, obviously. If you want it to work on your textbox remove or comment this line
param.ExStyle |= 0x08000000;
and it will work fine in your app window not others:
private void button14_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Focus();
SendKeys.Send("Q");
}
else
{
textBox1.Focus();
SendKeys.Send("q");
}
}
For WPF applications, you have to use SendKeys.SendWait() method.
SendKeys.SendWait("Q")
SendKeys.Send() will work for WinForm Applications.
Another option is to use WinAPI instead of SendKeys. More information here
Edit 1
Control focusedC;
//Enter event handler for your TextBox
private void textBox1_TextChanged(object sender, EventArgs e)
{
focusedC = sender as TextBox;
}
//Click event handler
private void button14_Click(object sender, EventArgs e)
{
if (focusedC != null)
{
focusedC.Focus();
SendKeys.Send("Q");
}
}
Edit 2: Using WinAPI
[DllImport("user32.dll")]
static extern void SendInput(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
public static void PressKey(byte keyCode)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
SendInput((byte)keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
SendInput((byte)keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
Use by calling the PressKey function and Keycodes can be found here

How to make a picturebox dragable?

I am an enthusiastic programmer and new to stack overflow.
I am trying to build a prototype of an OS(Operating System) in C#...Just a test.
Just like we see that we can drag things in the desktop and put it anywhere I am creating a desktop for my OS.
So how should I make an icon(a picturebox) drag-able and how would I save its position so that next time I open my desktop I see it in the same place?
I would love the dragging without any freezes or those sneaky bugs. I would love if, it is as close and smooth as the ones in Windows(dragging items(icons) in desktop)..
Thanks...
Yes, it is.
Assume a Picturebox named "pbxBigCat" (load it with a pPicture ...)
Add this lines to your form:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
And then write an eventhandler for the pbxBigCat MouseDown event:
private void pbxBigCat_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(pbxBigCat.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
If you test it you will se it works. For more Information (like saving positions etc.) I refere to Make a borderless form movable?
Another possibility to do this (now we have a Label called label1)
public partial class Form1 : Form
{
private bool mouseDown;
private Point lastLocation;
public Form1()
{
InitializeComponent();
}
private void pbxBigCat_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDown = true;
lastLocation = e.Location;
}
}
private void pbxBigCat_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
pbxBigCat.Location = new Point((pbxBigCat.Location.X - lastLocation.X + e.X), (pbxBigCat.Location.Y - lastLocation.Y) + e.Y);
label1.Text = pbxBigCat.Location.X.ToString() + "/" + pbxBigCat.Location.Y.ToString();
}
}
private void pbxBigCat_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
}
Everything drawn from the example on the mentioned SO article.

Shake detection mouse

My code is used to form1
private int X,Y;
private void timer1_Tick(object sender, EventArgs e)
{
if (this.X != Cursor.Position.X ||
this.Y != Cursor.Position.Y)
{
this.Form1_Load(this, e);
}
else
{
this.Text = (Convert.ToInt16(this.Text)+1).ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "0";
this.X = Cursor.Position.X;
this.Y = Cursor.Position.Y;
}
Other forms will not answer
If the form2 is open. The above code does not work.
Well I am not sure what exactly want to accomplish, but try to set form2.Owner = this (if you show form2 from from1) or form2.Owner = form1.
This code for the screensaver :
//inForm1
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
//...
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
//...
public static void SetScreenSaverRunning()
{
SendMessage
(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}
public void Shakedetectionmouse(EventArgs e)
{
//CallTimer1_Tick
timer1_Tick(this,e);
}
private void timer1_Tick(object sender, EventArgs e)
{
//ShowScreenSaver
timer1.Enabled = true;
timer1.Interval = 1000;
SetScreenSaverRunning();
}
private void button1_Click(object sender, EventArgs e)
{
//ShowForm2
using (var Form2 = new Form2())
{
Form2.ShowDialog();
}
}
//Inform2
private void Form2_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 2000;
}
private void timer1_Tick(object sender, EventArgs e)
{
using(var form1 = new Form1())
{
form1.Shakedetectionmouse(EventArgs.Empty);
}
}
In any form should be used Timer Component
Is it not a way to solve this issue?
screen saver Windows can be used?

Categories