Microsoft MVP성태의 닷넷 이야기
Windows: 148. Windows - Raw Input의 Top level collection 의미 [링크 복사], [링크+제목 복사],
조회: 13206
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

(시리즈 글이 9개 있습니다.)
Windows: 148. Windows - Raw Input의 Top level collection 의미
; https://www.sysnet.pe.kr/2/0/11612

.NET Framework: 788. RawInput을 이용한 키보드/마우스 입력 모니터링
; https://www.sysnet.pe.kr/2/0/11615

개발 환경 구성: 488. (User-mode 코드로 가상 USB 장치를 만들 수 있는) USB/IP PROJECT 소개
; https://www.sysnet.pe.kr/2/0/12213

개발 환경 구성: 490. C# - (Wireshark의) USBPcap을 이용한 USB 패킷 모니터링
; https://www.sysnet.pe.kr/2/0/12215

.NET Framework: 904. USB/IP PROJECT를 이용해 C#으로 USB Keyboard 가상 장치 만들기
; https://www.sysnet.pe.kr/2/0/12216

.NET Framework: 905. C# - DirectX 게임 클라이언트 실행 중 키보드 입력을 감지하는 방법
; https://www.sysnet.pe.kr/2/0/12218

.NET Framework: 917. C# - USB 관련 ETW(Event Tracing for Windows)를 이용한 키보드 입력을 감지하는 방법
; https://www.sysnet.pe.kr/2/0/12246

.NET Framework: 990. C# - SendInput Win32 API를 이용한 가상 키보드/마우스
; https://www.sysnet.pe.kr/2/0/12469

개발 환경 구성: 607. 로컬의 USB 장치를 원격 머신에 제공하는 방법 - usbip-win
; https://www.sysnet.pe.kr/2/0/12858




Windows - Raw Input의 Top level collection 의미

오호~~~ 아래와 같은 질문이 있는데,

windows7 C# SetWindowHookex
; https://social.msdn.microsoft.com/Forums/ko-KR/27e36513-c0b6-4bb0-8525-5a4ce00fa7cf/windows7-c-setwindowhookex?forum=visualcsharpko

저도 처음 알았습니다. Windows 7에는,

Global hooks getting lost on Windows 7
; https://learn.microsoft.com/en-us/archive/blogs/alejacma/global-hooks-getting-lost-on-windows-7

다음의 제약이 있습니다.

On Windows 7 we have to make sure that the callback function of the hook can return in less than LowLevelHooksTimeout, which is 300 ms. And we allow for the application to be timed out 10 times when processing the hook callback message. If it times out an 11th time, Windows will unhook the application from the hook chain. This is a by design feature and it was added in Win7 RTM.


시나리오가 그려지는군요. ^^ SetWindowsHookEx을 마련해놨는데, 응용 프로그램 개발자들이 그것의 구현을 잘못하게 되면 시스템의 키보드/마우스의 반응이 함께 느려지는 것입니다. 사용자들은, 어떤 응용 프로그램이 SetWindowsHookEx를 사용하고 있는지 알 수 없으므로 결국에는 윈도우 운영체제의 결함으로 느끼게 되는 것입니다. 아마도... 이런 상황을 더는 두고 볼 수가 없었겠지요. 그래서 윈도우 운영체제가 HOOKPROC 콜백 함수를 호출했을 때 300ms 이상 지연되는 횟수가 11회까지 되면 해당 HOOK을 제거한다는 것입니다.




이와 함께, SetWindowsHookEx보다는 Raw Input을 이용한 방법을 추천하고 있습니다. 물론, 그 둘 간의 차이는 있습니다. SetWindowsHookEx는 키보드 입력 시 응용 프로그램에 전달하지 못하도록 막는 것도 가능하지만, Raw Input 관련 기술의 경우 막는 것까지는 안 되고, 모니터링은 가능하다는 것입니다.

어쨌든, 모니터링 정도만 원한다면 굳이 SetWindowsHookEx보다는 Raw Input을 사용하는 것은 좋은 의도로 보입니다.

Using Raw Input
; https://learn.microsoft.com/en-us/windows/desktop/inputdev/using-raw-input

그런데 위의 사용법에 보면 usUsagePage와 usUsage 속성에 넣는 값이 좀 낯섭니다.

RAWINPUTDEVICE Rid[2];
        
Rid[0].usUsagePage = 0x01; 
Rid[0].usUsage = 0x02; 
Rid[0].dwFlags = RIDEV_NOLEGACY;   // adds HID mouse and also ignores legacy mouse messages
Rid[0].hwndTarget = 0;

Rid[1].usUsagePage = 0x01; 
Rid[1].usUsage = 0x06; 
Rid[1].dwFlags = RIDEV_NOLEGACY;   // adds HID keyboard and also ignores legacy keyboard messages
Rid[1].hwndTarget = 0;

if (RegisterRawInputDevices(Rid, 2, sizeof(Rid[0])) == FALSE) {
    //registration failed. Call GetLastError for the cause of the error
}

관련 구조체의 도움말을 보면,

RAWINPUTDEVICE structure
; https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-rawinputdevice

usUsagePage - Top level collection Usage page for the raw input device. 
usUsage - Top level collection Usage for the raw input device. 

* TLC(top level collection)

더 낯설어서 별로 도움이 안 됩니다. ^^ 어쨌든, 예제 코드를 통해 다음의 설정으로 정리가 됩니다.

HID mouse
    usUsagepage = 0x01
    usUsage = 0x02

HID keyboard
    usUsagepage = 0x01
    usUsage = 0x06

game pad
    usUsagepage = 0x01
    usUsage = 0x05

joystick
    usUsagepage = 0x01
    usUsage = 0x04

그런데, 보고 있자니 다음의 글이 생각납니다.

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법 (두 번째 이야기)
; https://www.sysnet.pe.kr/2/0/11363

위의 글에서 HID 장치에 대한 Report Descriptors를 작성한 것이 나오는데요, 바로 여기에 USAGE_PAGE와 USAGE가 있습니다.

05, 01, USAGE_PAGE (Generic Desktop)
09, 06, USAGE (Keyboard)
a1, 01, COLLECTION (Application)
...[생략]...
C0      END_COLLECTION

05, 01  USAGE_PAGE (Generic Desktop)
09, 02  USAGE (Mouse)
a1, 01  COLLECTION (Application)
...[생략]...
C0      END_COLLECTION

아하... 그러니까 위에서도 키보드인 경우 USAGE_PAGE == 1, USAGE == 6을 지정했고 마우스인 경우 USAGE_PAGE는 같고 USAGE 값이 2입니다. 정확히 일치하는군요. 결국 저 값들에 대한 정보는 다음의 문서에서 구할 수 있습니다.

Top-Level Collections
; https://learn.microsoft.com/en-us/windows-hardware/drivers/hid/top-level-collections

Top-Level Collections Opened by Windows for System Use
; https://learn.microsoft.com/en-us/windows-hardware/drivers/hid/top-level-collections-opened-by-windows-for-system-use




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 4/29/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
11920정성태5/28/201910059.NET Framework: 839. C# - PLplot 색상 제어
11919정성태5/27/201913195Math: 54. C# - 최소 자승법의 1차 함수에 대한 매개변수를 단순 for 문으로 구하는 방법 [1]파일 다운로드1
11918정성태5/25/201914379Math: 53. C# - 행렬식을 이용한 최소 자승법(LSM: Least Square Method)파일 다운로드1
11917정성태5/24/201914509Math: 52. MathNet을 이용한 간단한 통계 정보 처리 - 분산/표준편차파일 다운로드1
11916정성태5/24/201912503Math: 51. MathNET + OxyPlot을 이용한 간단한 통계 정보 처리 - Histogram파일 다운로드1
11915정성태5/24/201914802Linux: 11. 리눅스의 환경 변수 관련 함수 정리 - putenv, setenv, unsetenv
11914정성태5/24/201914499Linux: 10. 윈도우의 GetTickCount와 리눅스의 clock_gettime파일 다운로드1
11913정성태5/23/201912132.NET Framework: 838. C# - 숫자형 타입의 bit(2진) 문자열, 16진수 문자열 구하는 방법파일 다운로드1
11912정성태5/23/201911785VS.NET IDE: 137. Visual Studio 2019 버전 16.1부터 리눅스 C/C++ 프로젝트에 추가된 WSL 지원
11911정성태5/23/201910862VS.NET IDE: 136. Visual Studio 2019 - 리눅스 C/C++ 프로젝트에 인텔리센스가 동작하지 않는 경우
11910정성태5/23/201919515Math: 50. C# - MathNet.Numerics의 Matrix(행렬) 연산 [1]파일 다운로드1
11909정성태5/22/201913941.NET Framework: 837. C# - PLplot 사용 예제 [1]파일 다운로드1
11908정성태5/22/201912332.NET Framework: 836. C# - Python range 함수 구현파일 다운로드1
11907정성태5/22/201910123오류 유형: 541. msbuild - MSB4024 The imported project file "...targets" could not be loaded
11906정성태5/21/201910081.NET Framework: 835. .NET Core/C# - 리눅스 syslog에 로그 남기는 방법
11905정성태5/21/201910755.NET Framework: 834. C# - 폴더 경로 문자열에서 "..", "." 표기를 고려한 최종 문자열을 얻는 방법 - 두 번째 이야기
11904정성태5/21/201917025.NET Framework: 833. C# - Open Hardware Monitor를 이용한 CPU 온도 정보 [1]파일 다운로드1
11903정성태5/21/201911979오류 유형: 540. .NET Core - System.PlatformNotSupportedException: The named version of this synchronization primitive is not supported on this platform.
11902정성태5/21/201911128오류 유형: 539. mstest 실행 시 "The directory name is invalid." 오류 발생
11901정성태5/21/201912280오류 유형: 538. msbuild 오류 - Could not find a part of the path '%LOCALAPPDATA%\Temp\2\.NETFramework,Version=v4.0.AssemblyAttributes.cs'
11900정성태5/18/201911551오류 유형: 537. "sfc /scannow" 실행 중 시스템이 부팅되는 현상
11899정성태5/17/201912583Linux: 9. Linux에서 윈도우의 OutputDebugString 대신 사용할 수 있는 syslog [1]
11898정성태5/16/201913959VC++: 130. C++ string의 c_str과 data 함수의 차이점 [3]
11897정성태5/16/201920626오류 유형: 536. Visual Studio - "Developer Pack"을 설치했는데도 "대상 프레임워크" 목록에 나오지 않는 경우 [2]
11896정성태5/15/201915345개발 환경 구성: 440. C#, C++ - double의 Infinity, NaN 표현 방식파일 다운로드1
11895정성태5/12/201913739.NET Framework: 832. ML.NET Model Builder - 회귀(Regression), 다중 분류(Multi-class classification) 예제파일 다운로드1
... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...