Microsoft MVP성태의 닷넷 이야기
Windows: 148. Windows - Raw Input의 Top level collection 의미 [링크 복사], [링크+제목 복사],
조회: 13209
글쓴 사람
정성태 (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)
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
11894정성태5/10/201914882VS.NET IDE: 135. Visual Studio - ML.NET Model Builder 소개 [5]
11893정성태5/10/201912502오류 유형: 535. C# 6.0 이상의 문법을 컴파일 시 오류가 발생한다면?
11892정성태5/10/201912544웹: 38. HTTP Cookie의 expires 시간 형식(RFC7231)
11891정성태5/9/201915025.NET Framework: 831. (번역글) .NET Internals Cookbook Part 12 - Memory structure, attributes, handles
11890정성태5/8/201910783개발 환경 구성: 439. "Visual Studio Enterprise is required to execute the test." 메시지와 관련된 코드 기록
11889정성태5/8/201911640개발 환경 구성: 438. mstest, QTAgent의 로그 파일 설정 방법
11888정성태5/8/201926873.NET Framework: 830. C# - 비동기 호출을 취소하는 CancellationToken의 간단한 예제 코드 [1]파일 다운로드1
11887정성태5/8/201913366.NET Framework: 829. C# - yield 문을 사용할 수 있는 메서드의 조건
11886정성태5/7/201912434오류 유형: 534. mstest.exe 실행 시 "Visual Studio Enterprise is required to execute the test." 오류 [2]
11885정성태5/7/20199397오류 유형: 533. mstest.exe 실행 시 "File extension specified '.loadtest' is not a valid test extension." 오류 발생
11884정성태5/5/201913076.NET Framework: 828. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 두 번째 이야기
11883정성태5/3/201917391.NET Framework: 827. C# - 인터넷 시간 서버로부터 받은 시간을 윈도우에 적용하는 방법파일 다운로드1
11882정성태5/2/201914299.NET Framework: 826. (번역글) .NET Internals Cookbook Part 11 - Various C# riddles파일 다운로드1
11881정성태4/28/201914545오류 유형: 532. .NET Core 프로젝트로 마이그레이션 시 "CS0579 Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute" 오류 발생
11880정성태4/25/201911235오류 유형: 531. 이벤트 로그 오류 - Task Scheduling Error: m->NextScheduledSPRetry 1547, m->NextScheduledEvent 1547
11879정성태4/24/201916465.NET Framework: 825. (번역글) .NET Internals Cookbook Part 10 - Threads, Tasks, asynchronous code and others파일 다운로드2
11878정성태4/22/201914471.NET Framework: 824. (번역글) .NET Internals Cookbook Part 9 - Finalizers, queues, card tables and other GC stuff파일 다운로드1
11877정성태4/22/201914430.NET Framework: 823. (번역글) .NET Internals Cookbook Part 8 - C# gotchas파일 다운로드1
11876정성태4/21/201914262.NET Framework: 822. (번역글) .NET Internals Cookbook Part 7 - Word tearing, locking and others파일 다운로드1
11875정성태4/21/201914625오류 유형: 530. Visual Studo에서 .NET Core 프로젝트를 열 때 "One or more errors occurred." 오류 발생
11874정성태4/20/201914631.NET Framework: 821. (번역글) .NET Internals Cookbook Part 6 - Object internals파일 다운로드1
11873정성태4/19/201913590.NET Framework: 820. (번역글) .NET Internals Cookbook Part 5 - Methods, parameters, modifiers파일 다운로드1
11872정성태4/17/201914055.NET Framework: 819. (번역글) .NET Internals Cookbook Part 4 - Type members파일 다운로드1
11871정성태4/16/201913910.NET Framework: 818. (번역글) .NET Internals Cookbook Part 3 - Initialization tricks [3]파일 다운로드1
... 61  62  63  64  65  66  67  68  69  [70]  71  72  73  74  75  ...