Programm_Wirl/VirtualKeyboard/QwertzKeyboard.cs

309 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
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]};
// **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↵" };
}
if (numeric == "isCalc")
{
isCalc = true;
numpadLayout = new string[] {"⌫/*-","789+","456","123↵","0,"};
}
if (numeric == "isAlpha") isNumeric = isCalc = false;
targetTextBox = target;
InitializeKeyboard();
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 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);
UpdateLayout();
}
private void OK_Click(object sender, EventArgs e)
{
this.Close();
}
private void Tab_Click(object sender, EventArgs e)
{
DialogResult result = DialogResult.None;
if (targetTextBox.Parent?.SelectNextControl(targetTextBox, true, true, true, true) == true) result = DialogResult.OK;
else result = DialogResult.Cancel;
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;
UpdateLayout();
}
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
}
}
}
else
{
// **VERSTECKE ALLE QWERTZ-Buttons** und zeige nur NumPad
HideAllQwertzButtons();
SetupRealNumPad();
}
this.Refresh();
targetTextBox.Focus();
}
private void HideAllQwertzButtons()
{
// Blende ALLE Keys aus (QWERTZ komplett weg)
foreach (var row in keys)
{
foreach (var btn in row.Where(b => b != null))
{
btn.Visible = false;
}
}
}
private void SetupRealNumPad()
{
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);
// NumPad-Grids füllen (erste 4 Reihen)
for (int row = 0; row < numpadLayout.Length; row++)
{
string rowText = numpadLayout[row];
btnHeight = 55;
xBase = 10;
for (int col = 0; col < rowText.Length; col++)
{
char ch = rowText[col];
if (ch == ' ') { xBase += btnWidth + spacing; continue; } // Leerspace für breiten 0
if (ch == '↵')
{
Button okBtn = keys[row][col];
// OK-Button rechts unten (NumPad-Enter-Stil)
okBtn.Text = "OK";
okBtn.Size = (isCalc) ? new Size(btnWidth, btnHeight * 2 + spacing) : new Size(btnWidth, btnHeight);
okBtn.Location = new Point(xBase, y);
okBtn.BackColor = Color.FromArgb(0, 192, 0);
okBtn.ForeColor = Color.White;
okBtn.Font = new Font("Arial", 14, FontStyle.Bold);
okBtn.FlatStyle = FlatStyle.Flat;
okBtn.FlatAppearance.BorderSize = 1;
okBtn.FlatAppearance.BorderColor = Color.Green;
okBtn.Visible = true;
okBtn.Click += OK_Click;
continue;
}
if (ch == '+') { btnHeight = btnHeight * 2 + spacing; }
Button btn = keys[row][col];
btn.Text = ch.ToString();
btn.Tag = ch;
btn.Size = (row == 4 && col == 0) ? new Size(btnWidth * 2 + spacing, btnHeight) : new Size(btnWidth, btnHeight); // Breiter 0
btn.Location = new Point(xBase, y);
btn.Font = new Font("Arial", 18, FontStyle.Bold);
btn.BackColor = Color.FromArgb(240, 240, 240); // NumPad-Look
btn.FlatStyle = FlatStyle.Flat;
btn.FlatAppearance.BorderSize = 1;
btn.Visible = true;
btn.Enabled = true;
xBase += btn.Width + spacing;
}
y += (btnHeight > 55) ? (btnHeight / 2) + (spacing / 2) : btnHeight + spacing;
}
// Verstecke ungenutzte Keys (Reihen 4+)
for (int row = 5; row < keys.Length; row++)
for (int col = 0; col < keys[row].Length; col++)
keys[row][col].Visible = false;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.Location = new Point((screen.WorkingArea.Width - this.Width) / 2, screen.WorkingArea.Height - this.Height);
}
}
}