Microsoft MVP성태의 닷넷 이야기
Windows: 148. Windows - Raw Input의 Top level collection 의미 [링크 복사], [링크+제목 복사],
조회: 13217
글쓴 사람
정성태 (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)
11796정성태12/19/201810230개발 환경 구성: 424. vcpkg - "File does not have expected hash" 오류를 무시하는 방법
11795정성태12/19/201812656Windows: 154. PowerShell - Zone 별로 DNS 레코드 유형 정보 조회 [1]
11794정성태12/16/201810010오류 유형: 508. Get-AzureWebsite : Request to a downlevel service failed.
11793정성태12/16/201811649개발 환경 구성: 423. NuGet 패키지 제작 - Native와 Managed DLL을 분리하는 방법 [1]
11792정성태12/11/201812407Graphics: 34. .NET으로 구현하는 OpenGL (11) - Per-Pixel Lighting파일 다운로드1
11791정성태12/11/201812415VS.NET IDE: 130. C/C++ 프로젝트의 시작 프로그램으로 .NET Core EXE를 지정하는 경우 닷넷 디버깅이 안 되는 문제 [1]
11790정성태12/11/201810735오류 유형: 507. Could not save daemon configuration to C:\ProgramData\Docker\config\daemon.json: Access to the path 'C:\ProgramData\Docker\config' is denied.
11789정성태12/10/201820641Windows: 153. C# - USB 장치의 연결 및 해제 알림을 위한 WM_DEVICECHANGE 메시지 처리 [2]파일 다운로드2
11788정성태12/4/201810552오류 유형: 506. SqlClient - Value was either too large or too small for an Int32.Couldn't store <2151292191> in ... Column
11787정성태11/29/201814528Graphics: 33. .NET으로 구현하는 OpenGL (9), (10) - OBJ File Format, Loading 3D Models파일 다운로드1
11786정성태11/29/201811172오류 유형: 505. OpenGL.NET 예제 실행 시 "Managed Debugging Assistant 'CallbackOnCollectedDelegate'" 예외 발생
11785정성태11/21/201813598디버깅 기술: 120. windbg 분석 사례 - ODP.NET 사용 시 Finalizer에서 System.AccessViolationException 예외 발생으로 인한 비정상 종료
11784정성태11/18/201813290Graphics: 32. .NET으로 구현하는 OpenGL (7), (8) - Matrices and Uniform Variables, Model, View & Projection Matrices파일 다운로드1
11783정성태11/18/201811384오류 유형: 504. 윈도우 환경에서 docker가 설치된 컴퓨터 간의 ping IP 주소 풀이 오류
11782정성태11/18/201811153Windows: 152. 윈도우 10에서 사라진 "Adapters and Bindings" 네트워크 우선순위 조정 기능 - 두 번째 이야기
11781정성태11/17/201813365개발 환경 구성: 422. SFML.NET 라이브러리 설정 방법 [1]파일 다운로드1
11780정성태11/17/201814841오류 유형: 503. vcpkg install bzip2 빌드 에러 - "Error: Building package bzip2:x86-windows failed with: BUILD_FAILED"
11779정성태11/17/201815227개발 환경 구성: 421. vcpkg 업데이트 [1]
11778정성태11/14/201813013.NET Framework: 803. UWP 앱에서 한 컴퓨터(localhost, 127.0.0.1) 내에서의 소켓 연결
11777정성태11/13/201812031오류 유형: 502. Your project does not reference "..." framework. Add a reference to "..." in the "TargetFrameworks" property of your project file and then re-run NuGet restore.
11776정성태11/13/201811280.NET Framework: 802. Windows에 로그인한 계정이 마이크로소프트의 계정인지, 로컬 계정인지 알아내는 방법
11775정성태11/13/201813828Graphics: 31. .NET으로 구현하는 OpenGL (6) - Texturing파일 다운로드1
11774정성태11/8/201811686Graphics: 30. .NET으로 구현하는 OpenGL (4), (5) - Shader파일 다운로드1
11773정성태11/7/201811449Graphics: 29. .NET으로 구현하는 OpenGL (3) - Index Buffer파일 다운로드1
11772정성태11/6/201813731Graphics: 28. .NET으로 구현하는 OpenGL (2) - VAO, VBO파일 다운로드1
11771정성태11/5/201812905사물인터넷: 56. Audio Jack 커넥터의 IR 적외선 송신기 - 두 번째 이야기 [1]
... 61  62  63  64  65  66  67  68  69  70  71  72  73  [74]  75  ...