LoginSignup
0
0

More than 1 year has passed since last update.

cscの作法 その276

Posted at

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

RegisterRawInputDevices使ってください。

方針

  • マウスとキーボードやる。

サンプルコード

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace App
{
	public partial class Form1 : Form {
		[DllImport("user32.dll")]
		private static extern int RegisterRawInputDevices(RawInputDevice[] devices, int number, int size);
		[DllImport("user32.dll")]
		private static extern int GetRawInputData(IntPtr rawInput, int command, out RawInput data, ref int size, int headerSize);
		private struct RawInputDevice {
			public short UsagePage;
			public short Usage;
			public int Flags;
			public IntPtr Target;
		}
		private struct RawInputHeader {
			public int Type;
			public int Size;
			public IntPtr Device;
			public IntPtr WParam;
		}
		private struct RawInput {
			public RawInputHeader Header;
			public short a;
			public short b;
			public short c;
			public ushort d;
			public ushort e;
			public int f;
			public int g;
			public int h;
		}
		private TextBox textBox1;
		public Form1() {
			textBox1 = new TextBox();
			textBox1.Location = new System.Drawing.Point(30, 30);
			textBox1.Multiline = true;
			textBox1.Size = new System.Drawing.Size(200, 40);
			textBox1.TabIndex = 2;
			Controls.AddRange(new Control[] {
				textBox1
			});
			const int RIDEV_INPUTSINK = 0x00000100;
			int size = Marshal.SizeOf(typeof(RawInputDevice));
			RawInputDevice[] devices = new RawInputDevice[2];
			devices[0].UsagePage = 1;
			devices[0].Usage = 2;
			devices[0].Target = this.Handle;
			devices[0].Flags = RIDEV_INPUTSINK;
			devices[1].UsagePage = 1;
			devices[1].Usage = 6;
			devices[1].Target = this.Handle;
			devices[1].Flags = RIDEV_INPUTSINK;
			RegisterRawInputDevices(devices, 2, size);
		}
		private void log(string msg) {
			Console.WriteLine(msg);
			textBox1.Text = msg;
		}
		protected override void WndProc(ref Message m) {
			const int WmInput = 0xFF;
			if (m.Msg == WmInput)
			{
				const int RidInput = 0x10000003;
				int headerSize = Marshal.SizeOf(typeof(RawInputHeader));
				int size = Marshal.SizeOf(typeof(RawInput));
				RawInput input;
				GetRawInputData(m.LParam, RidInput, out input, ref size, headerSize);
				string str = "";
				if (input.Header.Type == 0)
				{
					short ButtonData = input.c;
					sbyte delta = (sbyte) (input.d);
					int LastX = input.f;
					int LastY = input.g;
					str = string.Format("{0},{1},{2},{3},{4},{5}\r\n", "Mouse", input.Header.Device, LastX, LastY, ButtonData, delta);
					log(str);
				}
				else if (input.Header.Type == 1)
				{
					string message = "";
					ushort VKey = input.d;
					if (input.e == 256)
					{
						message = "KeyDown";
					}
					else if (input.e == 257)
					{
						message = "KeyUp";
					}
					str = string.Format("{0},{1},{2}\r\n", message, input.Header.Device, VKey);
					log(str);
				}
			}
			base.WndProc(ref m);
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0