-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
156 lines (127 loc) · 5.68 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Interop;
using Microsoft.Web.WebView2.Core;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace RawInput.Touchpad
{
public partial class MainWindow : Window
{
public bool TouchpadExists
{
get { return (bool)GetValue(TouchpadExistsProperty); }
set { SetValue(TouchpadExistsProperty, value); }
}
public static readonly DependencyProperty TouchpadExistsProperty =
DependencyProperty.Register("TouchpadExists", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));
public string TouchpadContacts
{
get { return (string)GetValue(TouchpadContactsProperty); }
set { SetValue(TouchpadContactsProperty, value); }
}
public static readonly DependencyProperty TouchpadContactsProperty =
DependencyProperty.Register("TouchpadContacts", typeof(string), typeof(MainWindow), new PropertyMetadata(null));
public MainWindow()
{
InitializeComponent();
webView.NavigationCompleted += OnTouchpadContactsChanged;
webView.WebMessageReceived += WebView_WebMessageReceived;
string currentDir = AppDomain.CurrentDomain.BaseDirectory;
string indexPath = Path.Combine(currentDir, "index.html"); // Assuming the HTML is in the root
// Construct a URI pointing to the local HTML file:
Uri localHtmlUri = new Uri($"file:///{indexPath.Replace("\\", "/")}");
// Navigate the WebView2 to the file URI:
webView.Source = localHtmlUri;
// เริ่มการตรวจจับการเปลี่ยนแปลงของ TouchpadContacts
DependencyPropertyDescriptor.FromProperty(TouchpadContactsProperty, typeof(MainWindow))
.AddValueChanged(this, OnTouchpadContactsChanged);
}
private void OnTouchpadContactsChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(TouchpadContacts))
{
try
{
// ใช้ Regular Expression ในการแยกข้อมูล
var regex = new Regex(@"Contact ID:(\d+) Point:(\d+),(\d+)");
var matches = regex.Matches(TouchpadContacts);
// สร้าง List ของ Object สำหรับเก็บข้อมูล
var contactsList = new List<object>();
foreach (Match match in matches)
{
int id = int.Parse(match.Groups[1].Value);
int x = int.Parse(match.Groups[2].Value);
int y = int.Parse(match.Groups[3].Value);
contactsList.Add(new { ID = id, X = x, Y = y });
}
// แปลง List เป็น JSON string
var dataObject = new { data = contactsList };
//string jsonContacts = JsonConvert.SerializeObject(contactsList);
string jsonContacts = JsonConvert.SerializeObject(dataObject);
//string jsonContacts = string.Join(",", dataObject.Select(JsonConvert.SerializeObject));
// แสดงค่า jsonContacts ใน Output Window
Debug.WriteLine("jsonContacts: " + jsonContacts);
// ส่งข้อมูล JSON ไปยัง WebView2
webView.CoreWebView2.PostWebMessageAsJson(jsonContacts);
}
catch (Exception ex)
{
// จัดการข้อผิดพลาด (เช่น แสดงข้อความเตือน)
MessageBox.Show("Error processing TouchpadContacts: " + ex.Message);
// แสดงข้อผิดพลาดใน Output Window
Debug.WriteLine("Error: " + ex.Message);
}
}
}
//new
private void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
if (e.IsSuccess)
{
string newText = "This text is set from C#!";
string jsonData = $"{{\"message\": \"{newText}\"}}";
webView.CoreWebView2.PostWebMessageAsJson(jsonData);
}
}
private void WebView_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
string responseMessage = e.WebMessageAsJson;
// Process the response message from JavaScript here
}
//new
private HwndSource _targetSource;
private readonly List<string> _log = new();
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
_targetSource = PresentationSource.FromVisual(this) as HwndSource;
_targetSource?.AddHook(WndProc);
TouchpadExists = TouchpadHelper.Exists();
_log.Add($"Precision touchpad exists: {TouchpadExists}");
if (TouchpadExists)
{
var success = TouchpadHelper.RegisterInput(_targetSource.Handle);
_log.Add($"Precision touchpad registered: {success}");
}
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case TouchpadHelper.WM_INPUT:
var contacts = TouchpadHelper.ParseInput(lParam);
TouchpadContacts = string.Join(Environment.NewLine, contacts.Select(x => x.ToString()));
_log.Add("---");
_log.Add(TouchpadContacts);
break;
}
return IntPtr.Zero;
}
}
}