Microsoft MVP성태의 닷넷 이야기
Windows: 148. Windows - Raw Input의 Top level collection 의미 [링크 복사], [링크+제목 복사],
조회: 12970
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12413정성태11/17/202010646.NET Framework: 970. .NET 5 / .NET Core - UnmanagedCallersOnly 특성을 사용한 함수 내보내기파일 다운로드1
12412정성태11/16/202012888.NET Framework: 969. .NET Framework 및 .NET 5 - UnmanagedCallersOnly 특성 사용파일 다운로드1
12411정성태11/12/20209875오류 유형: 680. C# 9.0 - Error CS8889 The target runtime doesn't support extensible or runtime-environment default calling conventions.
12410정성태11/12/20209797디버깅 기술: 174. windbg - System.TypeLoadException 예외 분석 사례
12409정성태11/12/202011196.NET Framework: 968. C# 9.0의 Function pointer를 이용한 함수 주소 구하는 방법파일 다운로드1
12408정성태11/9/202022656도서: 시작하세요! C# 9.0 프로그래밍 [8]
12407정성태11/9/202011460.NET Framework: 967. "clr!JIT_DbgIsJustMyCode" 호출이 뭘까요?
12406정성태11/8/202013072.NET Framework: 966. C# 9.0 - (15) 최상위 문(Top-level statements) [5]파일 다운로드1
12405정성태11/8/202010514.NET Framework: 965. C# 9.0 - (14) 부분 메서드에 대한 새로운 기능(New features for partial methods)파일 다운로드1
12404정성태11/7/202011034.NET Framework: 964. C# 9.0 - (13) 모듈 이니셜라이저(Module initializers)파일 다운로드1
12403정성태11/7/202011095.NET Framework: 963. C# 9.0 - (12) foreach 루프에 대한 GetEnumerator 확장 메서드 지원(Extension GetEnumerator)파일 다운로드1
12402정성태11/7/202011689.NET Framework: 962. C# 9.0 - (11) 공변 반환 형식(Covariant return types) [1]파일 다운로드1
12401정성태11/5/202010603VS.NET IDE: 153. 닷넷 응용 프로그램에서의 "My Code" 범위와 "Enable Just My Code"의 역할 [1]
12400정성태11/5/20207595오류 유형: 679. Visual Studio - "Source Not Found" 창에 "Decompile source code" 링크가 없는 경우
12399정성태11/5/202011271.NET Framework: 961. C# 9.0 - (10) 대상으로 형식화된 조건식(Target-typed conditional expressions)파일 다운로드1
12398정성태11/4/20209757오류 유형: 678. Windows Server 2008 R2 환경에서 Powershell을 psexec로 원격 실행할 때 hang이 발생하는 문제
12397정성태11/4/20209899.NET Framework: 960. C# - 조건 연산자(?:)를 사용하는 경우 달라지는 메서드 선택 사례파일 다운로드1
12396정성태11/3/20208258VS.NET IDE: 152. Visual Studio - "Tools" / "External Tools..."에 등록된 외부 명령어에 대한 단축키 설정 방법
12395정성태11/3/20209591오류 유형: 677. SSMS로 DB 접근 시 The server principal "..." is not able to access the database "..." under the current security context.
12394정성태11/3/20207989오류 유형: 676. cacls - The Recycle Bin on ... is corrupted. Do you want to empty the Recycle Bin for this drive?
12393정성태11/3/20208474오류 유형: 675. Visual Studio - 닷넷 응용 프로그램 디버깅 시 Disassembly 창에서 BP 설정할 때 "Error while processing breakpoint." 오류
12392정성태11/2/202012598.NET Framework: 959. C# 9.0 - (9) 레코드(Records) [4]파일 다운로드1
12390정성태11/1/202010837디버깅 기술: 173. windbg - System.Configuration.ConfigurationErrorsException 예외 분석 방법
12389정성태11/1/202010631.NET Framework: 958. C# 9.0 - (8) 정적 익명 함수 (static anonymous functions)파일 다운로드1
12388정성태10/29/202010070오류 유형: 674. 어느 순간부터 닷넷 응용 프로그램 실행 시 System.Configuration.ConfigurationErrorsException 예외가 발생한다면?
12387정성태10/28/202010818.NET Framework: 957. C# - static 필드의 정보가 GC Heap에 저장될까요? [3]파일 다운로드1
... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...