Programm_Wirl/VirtualKeyboard/QwertzKeyboard.cs

300 lines
9.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace VirtualKeyboard
{
public partial class QwertzKeyboard : Form
{
private TextBox targetTextBox;
private Button okBtn;
private bool isNumeric = false;
private bool isCalc = false;
private bool isShift = false;
private Screen screen = Screen.PrimaryScreen;
// Erweiterte keys auf mehr Buttons (z.B. 14 pro Reihe)
private Button[][] keys = new Button[5][] {
new Button[14], new Button[14], new Button[14], new Button[14], new Button[8]};
private Button[][] numkeys = new Button[4][] {
new Button[4], new Button[4], new Button[4], new Button[4]};
// **Echtes QWERTZ-Layout** (5 Reihen + Funktion/Steuerung)
private string[] qwertzRows = {
"?1234567890ß⌫↵", // Ziffernreihe
"↔qwertzuiopü+", // Hauptbuchstaben 1 (versetzt)
" asdfghjklöä#", // Hauptbuchstaben 2 (versetzt)
"⇧<yxcvbnm,.-⇧", // Hauptbuchstaben 3 (Y statt Z, versetzt)
" _ " // Untere Reihe: Shift (breit), Enter (hoch), Space (sehr breit)
};
// NumPad-Positionen: Standard-Rechner-Layout
// Zeile 1: 7 8 9 /
// Zeile 2: 4 5 6 *
// Zeile 3: 1 2 3 -
// Zeile 4: 0 00 , +
// Rechts: OK
//string[] numpadLayout = {
// "⌫/*-",
// "789+",
// "456",
// "123↵",
// "0," // 0 breit, Komma, Plus
// };
// Numeric-Reihen
private string[] numpadLayout;
public QwertzKeyboard(TextBox target, string numeric)
{
if (numeric == "isNumeric")
{
isNumeric = true;
numpadLayout = new string[] { "789", "456", "123", "⌫0↵" };
InitializeNumPad();
}
if (numeric == "isCalc")
{
isCalc = true;
numpadLayout = new string[] {"⌫/*-","789+","456","123↵","0,"};
InitializeNumPad();
}
if (numeric == "isAlpha")
{
isNumeric = isCalc = false;
InitializeKeyboard();
}
this.targetTextBox = target;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.FromArgb(1,53,101);
this.StartPosition = FormStartPosition.Manual;
this.TopMost = true;
this.ShowInTaskbar = false;
this.targetTextBox.Focus();
}
private void InitializeNumPad()
{
int y = 10, xBase = 10, btnWidth = 70, btnHeight = 55, spacing = 8;
if (isCalc) this.Size = new Size((btnWidth + spacing) * 4 + xBase, (btnHeight + spacing) * 5 + xBase);
if (isNumeric) this.Size = new Size((btnWidth + spacing) * 3 + xBase, (btnHeight + spacing) * 4 + xBase);
for (int row = 0; row < numpadLayout.Length; row++)
{
string rowText = numpadLayout[row];
int rowLength = rowText.Length;
numkeys[row] = new Button[rowLength];
xBase = 10;
for (int col = 0;col < rowLength; col++)
{
char ch = rowText[col];
if (ch == ' ') { xBase += btnWidth + spacing; continue; } // Leerspace für breiten 0
if (ch == '↵')
{
Button okBtn = new Button
{
Text = "OK",
Size = (isCalc) ? new Size(btnWidth, btnHeight * 2 + spacing) : new Size(btnWidth, btnHeight),
Location = new Point(xBase, y),
BackColor = Color.FromArgb(0, 192, 0),
ForeColor = Color.White,
Font = new Font("Arial", 14, FontStyle.Bold),
FlatStyle = FlatStyle.Flat,
Visible = true,
};
okBtn.FlatAppearance.BorderSize = 1;
okBtn.FlatAppearance.BorderColor = Color.Green;
okBtn.Click += OK_Click;
this.Controls.Add(okBtn);
// OK-Button rechts unten (NumPad-Enter-Stil)
continue;
}
if (ch == '+') { btnHeight = btnHeight * 2 + spacing; }
Button btn = new Button
{
Text = ch.ToString(),
Tag = ch,
Size = new Size(btnWidth, btnHeight),
Location = new Point(xBase, y),
Font = new Font("Arial", 18, FontStyle.Bold),
BackColor = Color.FromArgb(240, 240, 240),
FlatStyle = FlatStyle.Flat,
Visible = true,
Enabled = true,
};
btn.Click += Key_Click;
this.Controls.Add(btn);
numkeys[row][col] = btn;
xBase += btn.Width + spacing;
}
y += btnHeight + spacing;
}
}
private void InitializeKeyboard()
{
int btnHeight = 60, spacing = 4;
int startY = 10;
for (int row = 0; row < qwertzRows.Length; row++)
{
string rowText = qwertzRows[row];
int rowLength = rowText.Length;
keys[row] = new Button[rowLength];
int x = 10;
for (int col = 0; col < rowLength; col++)
{
int btnWidth = 0; // Breiten anpassen
char ch = rowText[col];
string sch = ch.ToString();
int btnH = (ch == '↵' ? (btnHeight + spacing) * 5 - 4 : btnHeight); // Hoher Enter
Button btn = new Button
{
Text = sch,
Size = new Size(btnWidth, btnH),
Location = new Point(x, startY),
Font = new Font(ch == ' ' ? "Arial" : "Arial", 18, FontStyle.Bold),
Tag = ch,
BackColor = Color.FromArgb(240, 240, 240),
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 1, MouseOverBackColor = Color.LightGray },
};
btn.Click += Key_Click;
switch (btn.Text)
{
case var t when t == " ":
btn.Width = 58;
btn.Enabled = false;
break;
case var t when t == "_":
btn.Width = (btnHeight + spacing) * 12 - 4;
btn.Text = "Leertaste";
break;
case var t when t == "⇧":
btn.Width = (int)((double)btnHeight * 1.5);
btn.Text = "Shift";
break;
case var t when t == "↔":
btn.Width = (int)((double)btnHeight * 1.5);
btn.Text = "Tab";
break;
case var t when t == "⌫":
btn.Width = btnHeight * 2;
break;
case var t when t == "↵":
btn.Width = 90;
btn.Text = "OK";
btn.BackColor = Color.FromArgb(0, 192, 0);
btn.ForeColor = Color.White;
btn.FlatAppearance.BorderSize = 1;
btn.FlatAppearance.BorderColor = Color.Green;
break;
default:
btn.Width = btnHeight;
break;
}
this.Controls.Add(btn);
keys[row][col] = btn;
x += btn.Width + spacing;
}
startY += keys[row][0].Height + spacing;
}
// OK wird in Reihen integriert
this.Size = new Size((int)(screen.WorkingArea.Width * 0.80), (btnHeight + spacing) * 5 + 10);
this.MaximumSize = new Size(1000, (btnHeight + spacing) * 5 + 10);
}
private void OK_Click(object sender, EventArgs e)
{
this.Close();
}
private void Tab_Click(object sender, EventArgs e)
{
if (targetTextBox.Parent?.SelectNextControl(targetTextBox, true, true, true, true) == true) this.Close();
}
private void Key_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
char ch = (char)btn.Tag;
switch (btn.Text)
{
case var t when t == "Shift": Shift_Click(btn, null);
break;
case var t when t == "Tab": Tab_Click(btn, null);
break;
case var t when t == "OK": OK_Click(btn, null);
break;
case var t when t == "⌫":
if (targetTextBox.Text.Length > 0)
targetTextBox.Text = targetTextBox.Text.Substring(0, targetTextBox.Text.Length - 1);
break;
default: targetTextBox.Text += btn.Text;
break;
}
if (isShift && char.IsLetter(ch)) ch = char.ToUpper(ch);
//targetTextBox.Focus();
//targetTextBox.SelectionStart = targetTextBox.Text.Length;
}
private void ToggleMode_Click()
{
isNumeric = !isNumeric;
}
private void Shift_Click(object sender, EventArgs e)
{
Button shiftBtn = (Button)sender;
isShift = !isShift;
shiftBtn.BackColor = isShift ? Color.Yellow : Color.FromArgb(240, 240, 240);
UpdateLayout();
}
private void UpdateLayout()
{
if (!isNumeric)
{
// Zeige ALLE QWERTZ-Buttons
for (int row = 0; row < qwertzRows.Length; row++)
{
string rowText = qwertzRows[row];
for (int col = 0; col < keys[row].Length && col < rowText.Length; col++)
{
char ch = rowText[col];
if (keys[row][col].Text.Length > 1) continue;
keys[row][col].Text = isShift && char.IsLetter(ch) ? char.ToUpper(ch).ToString() : ch.ToString();
keys[row][col].Tag = ch;
keys[row][col].Visible = true; // QWERTZ sichtbar
}
}
}
this.Refresh();
targetTextBox.Focus();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.Location = new Point((screen.WorkingArea.Width - this.Width) / 2, screen.WorkingArea.Height - this.Height);
}
}
}