| skipped 370 lines |
371 | 371 | | except Exception as e: |
372 | 372 | | return None, e |
373 | 373 | | |
374 | | - | async def send_key_virtualkey(self, vk:str, is_pressed:bool, is_extended:bool, scancode_hint:int = None): |
| 374 | + | async def send_key_virtualkey(self, vk:str, is_pressed:bool, is_extended:bool, scancode_hint:int = None, modifiers = VK_MODIFIERS(0)): |
375 | 375 | | try: |
376 | | - | if indata.vk_code is not None: |
377 | | - | vk_code = indata.vk_code |
378 | | - | else: |
379 | | - | vk_code = self.__keyboard_layout.scancode_to_vk(indata.keyCode) |
380 | | - | print('Got VK: %s' % vk_code) |
381 | | - | if vk_code is None: |
382 | | - | print('Could not map SC to VK! SC: %s' % indata.keyCode) |
383 | | - | if vk_code is not None and vk_code in self.__vk_to_vnckey: |
384 | | - | keycode = self.__vk_to_vnckey[vk_code] |
| 376 | + | if vk is None: |
| 377 | + | return await self.send_key_scancode(scancode_hint, is_pressed, is_extended, modifiers=modifiers) |
| 378 | + | print('Got VK: %s' % vk) |
| 379 | + | if vk is None: |
| 380 | + | print('Could not map SC to VK! SC: %s' % scancode_hint) |
| 381 | + | if vk is not None and vk in self.__vk_to_vnckey: |
| 382 | + | keycode = self.__vk_to_vnckey[vk] |
385 | 383 | | print('AAAAAAAA %s' % hex(keycode)) |
386 | | - | |
387 | | - | |
388 | | - | if vk in self.__vk_to_sc: |
389 | | - | scancode = self.__vk_to_sc[vk] |
390 | | - | is_extended = True |
391 | | - | print('EXT') |
| 384 | + | if keycode is None: |
| 385 | + | return await self.send_key_scancode(scancode_hint, is_pressed, is_extended, modifiers=modifiers) |
392 | 386 | | else: |
393 | | - | scancode = scancode_hint |
394 | | - | return await self.send_key_char(scancode, is_pressed, is_extended) |
| 387 | + | return await self.send_key_char(keycode, is_pressed) |
395 | 388 | | except Exception as e: |
396 | 389 | | traceback.print_exc() |
397 | 390 | | return None, e |
398 | 391 | | |
399 | | - | async def send_key_scancode(self, scancode, is_pressed, is_extended): |
| 392 | + | async def send_key_scancode(self, scancode, is_pressed, is_extended, modifiers = VK_MODIFIERS(0)): |
400 | 393 | | try: |
401 | | - | keycode = self.__keyboard_layout.scancode_to_char(indata.keyCode, modifiers) |
402 | | - | print(keycode) |
| 394 | + | keycode = None |
| 395 | + | if modifiers == VK_MODIFIERS(0): |
| 396 | + | vk = self.__keyboard_layout.scancode_to_vk(scancode) |
| 397 | + | if vk is not None and vk in self.__vk_to_vnckey: |
| 398 | + | keycode = self.__vk_to_vnckey[vk] |
403 | 399 | | if keycode is None: |
404 | | - | print('Failed to resolv key! SC: %s VK: %s' % (indata.keyCode, vk_code)) |
405 | | - | #continue |
406 | | - | elif keycode is not None and len(keycode) == 1: |
407 | | - | keycode = ord(keycode) |
408 | | - | print('Keycode %s resolved to: %s' % (indata.keyCode , repr(keycode))) |
409 | | - | elif keycode is not None and len(keycode) > 1: |
410 | | - | print('LARGE! Keycode %s resolved to: %s' % (indata.keyCode , repr(keycode))) |
411 | | - | #continue |
412 | | - | else: |
413 | | - | print('This key is too special! Can\'t resolve it! SC: %s VK: %s' % (indata.keyCode, vk_code)) |
414 | | - | #continue |
| 400 | + | keycode = self.__keyboard_layout.scancode_to_char(scancode, modifiers) |
| 401 | + | if keycode is None: |
| 402 | + | raise Exception('Failed to resolv key! SC: %s MOD: %s' % (scancode, modifiers)) |
415 | 403 | | |
416 | | - | return True, None |
| 404 | + | elif keycode is not None and len(keycode) == 1: |
| 405 | + | keycode = ord(keycode) |
| 406 | + | elif keycode is not None and len(keycode) > 1: |
| 407 | + | raise Exception('LARGE! Keycode %s resolved to: %s' % (scancode , repr(keycode))) |
| 408 | + | else: |
| 409 | + | raise Exception('This key is too special! Can\'t resolve it! SC: %s' % scancode) |
| 410 | + | |
| 411 | + | return await self.send_key_char(keycode, is_pressed) |
417 | 412 | | except Exception as e: |
418 | 413 | | traceback.print_exc() |
419 | 414 | | return None, e |
| skipped 12 lines |
432 | 427 | | if xPos < 0 or yPos < 0: |
433 | 428 | | return True, None |
434 | 429 | | |
435 | | - | button =0 |
| 430 | + | buttoncode = 0 |
436 | 431 | | if button == MOUSEBUTTON.MOUSEBUTTON_LEFT: |
437 | | - | button = 1 |
| 432 | + | buttoncode = 1 |
438 | 433 | | elif button == MOUSEBUTTON.MOUSEBUTTON_MIDDLE: |
439 | | - | button = 2 |
| 434 | + | buttoncode = 2 |
440 | 435 | | elif button == MOUSEBUTTON.MOUSEBUTTON_RIGHT: |
441 | | - | button = 3 |
| 436 | + | buttoncode = 3 |
442 | 437 | | |
443 | 438 | | buttonmask = 0 |
444 | 439 | | if is_pressed is True: |
445 | | - | if button == 1: buttonmask &= ~1 |
446 | | - | if button == 2: buttonmask &= ~2 |
447 | | - | if button == 3: buttonmask &= ~4 |
448 | | - | if button == 4: buttonmask &= ~8 |
449 | | - | if button == 5: buttonmask &= ~16 |
| 440 | + | if buttoncode == 1: buttonmask &= ~1 |
| 441 | + | if buttoncode == 2: buttonmask &= ~2 |
| 442 | + | if buttoncode == 3: buttonmask &= ~4 |
| 443 | + | if buttoncode == 4: buttonmask &= ~8 |
| 444 | + | if buttoncode == 5: buttonmask &= ~16 |
450 | 445 | | else: |
451 | | - | if button == 1: buttonmask |= 1 |
452 | | - | if button == 2: buttonmask |= 2 |
453 | | - | if button == 3: buttonmask |= 4 |
454 | | - | if button == 4: buttonmask |= 8 |
455 | | - | if button == 5: buttonmask |= 16 |
| 446 | + | if buttoncode == 1: buttonmask |= 1 |
| 447 | + | if buttoncode == 2: buttonmask |= 2 |
| 448 | + | if buttoncode == 3: buttonmask |= 4 |
| 449 | + | if buttoncode == 4: buttonmask |= 8 |
| 450 | + | if buttoncode == 5: buttonmask |= 16 |
456 | 451 | | |
457 | 452 | | msg = pack("!BBHH", 5, buttonmask, xPos, yPos) |
458 | 453 | | self.__writer.write(msg) |
| skipped 34 lines |
493 | 488 | | return |
494 | 489 | | if indata.type == RDPDATATYPE.KEYSCAN: |
495 | 490 | | indata = cast(RDP_KEYBOARD_SCANCODE, indata) |
496 | | - | modifiers = VK_MODIFIERS(0) |
497 | | - | for mod in indata.modifiers: |
498 | | - | if mod == 'KEYPAD': |
499 | | - | modifiers |= VK_MODIFIERS.VK_NUMLOCK |
500 | | - | elif mod == 'SHIFT': |
501 | | - | modifiers |= VK_MODIFIERS.VK_SHIFT |
502 | | - | elif mod == 'CONTROL': |
503 | | - | modifiers |= VK_MODIFIERS.VK_CONTROL |
504 | | - | elif mod == 'ALT': |
505 | | - | modifiers |= VK_MODIFIERS.VK_MENU |
| 491 | + | if indata.vk_code is not None: |
| 492 | + | await self.send_key_virtualkey(indata.vk_code, indata.is_pressed, indata.is_extended, scancode_hint=indata.keyCode, modifiers=indata.modifiers) |
| 493 | + | else: |
| 494 | + | await self.send_key_scancode(indata.keyCode, indata.is_pressed, indata.is_extended, modifiers=indata.modifiers) |
506 | 495 | | |
507 | | - | ## emulating keys... |
508 | | - | keycode = None |
509 | | - | try: |
510 | | - | if indata.keyCode is None and indata.vk_code is None: |
511 | | - | print('No keycode found! ') |
512 | | - | continue |
513 | 496 | | |
514 | | - | if indata.vk_code is not None: |
515 | | - | vk_code = indata.vk_code |
516 | | - | else: |
517 | | - | vk_code = self.__keyboard_layout.scancode_to_vk(indata.keyCode) |
518 | | - | print('Got VK: %s' % vk_code) |
519 | | - | if vk_code is None: |
520 | | - | print('Could not map SC to VK! SC: %s' % indata.keyCode) |
521 | | - | if vk_code is not None and vk_code in self.__vk_to_vnckey: |
522 | | - | keycode = self.__vk_to_vnckey[vk_code] |
523 | | - | print('AAAAAAAA %s' % hex(keycode)) |
524 | 497 | | |
525 | | - | if keycode is None: |
526 | | - | keycode = self.__keyboard_layout.scancode_to_char(indata.keyCode, modifiers) |
527 | | - | print(keycode) |
528 | | - | if keycode is None: |
529 | | - | print('Failed to resolv key! SC: %s VK: %s' % (indata.keyCode, vk_code)) |
530 | | - | continue |
531 | | - | elif keycode is not None and len(keycode) == 1: |
532 | | - | keycode = ord(keycode) |
533 | | - | print('Keycode %s resolved to: %s' % (indata.keyCode , repr(keycode))) |
534 | | - | elif keycode is not None and len(keycode) > 1: |
535 | | - | print('LARGE! Keycode %s resolved to: %s' % (indata.keyCode , repr(keycode))) |
536 | | - | continue |
537 | | - | else: |
538 | | - | print('This key is too special! Can\'t resolve it! SC: %s VK: %s' % (indata.keyCode, vk_code)) |
539 | | - | continue |
540 | 498 | | |
541 | | - | except Exception as e: |
542 | | - | traceback.print_exc() |
543 | | - | continue |
544 | | - | |
545 | | - | if indata.keyCode is not None: |
546 | | - | print('Original kk : %s [%s]' % (indata.keyCode, hex(indata.keyCode))) |
547 | | - | print('Final keycode: %s' % hex(keycode)) |
548 | | - | print('Is pressed : %s' % indata.is_pressed) |
549 | | - | if keycode is not None: |
550 | | - | msg = pack("!BBxxI", 4, int(indata.is_pressed), keycode) |
551 | | - | self.__writer.write(msg) |
| 499 | + | #modifiers = indata.modifiers |
| 500 | + | # |
| 501 | + | ### emulating keys... |
| 502 | + | #keycode = None |
| 503 | + | #try: |
| 504 | + | # if indata.keyCode is None and indata.vk_code is None: |
| 505 | + | # print('No keycode found! ') |
| 506 | + | # continue |
| 507 | + | # |
| 508 | + | # if indata.vk_code is not None: |
| 509 | + | # vk_code = indata.vk_code |
| 510 | + | # else: |
| 511 | + | # vk_code = self.__keyboard_layout.scancode_to_vk(indata.keyCode) |
| 512 | + | # print('Got VK: %s' % vk_code) |
| 513 | + | # if vk_code is None: |
| 514 | + | # print('Could not map SC to VK! SC: %s' % indata.keyCode) |
| 515 | + | # if vk_code is not None and vk_code in self.__vk_to_vnckey: |
| 516 | + | # keycode = self.__vk_to_vnckey[vk_code] |
| 517 | + | # print('AAAAAAAA %s' % hex(keycode)) |
| 518 | + | # |
| 519 | + | # if keycode is None: |
| 520 | + | # keycode = self.__keyboard_layout.scancode_to_char(indata.keyCode, modifiers) |
| 521 | + | # print(keycode) |
| 522 | + | # if keycode is None: |
| 523 | + | # print('Failed to resolv key! SC: %s VK: %s' % (indata.keyCode, vk_code)) |
| 524 | + | # continue |
| 525 | + | # elif keycode is not None and len(keycode) == 1: |
| 526 | + | # keycode = ord(keycode) |
| 527 | + | # print('Keycode %s resolved to: %s' % (indata.keyCode , repr(keycode))) |
| 528 | + | # elif keycode is not None and len(keycode) > 1: |
| 529 | + | # print('LARGE! Keycode %s resolved to: %s' % (indata.keyCode , repr(keycode))) |
| 530 | + | # continue |
| 531 | + | # else: |
| 532 | + | # print('This key is too special! Can\'t resolve it! SC: %s VK: %s' % (indata.keyCode, vk_code)) |
| 533 | + | # continue |
| 534 | + | # |
| 535 | + | #except Exception as e: |
| 536 | + | # traceback.print_exc() |
| 537 | + | # continue |
| 538 | + | # |
| 539 | + | #if indata.keyCode is not None: |
| 540 | + | # print('Original kk : %s [%s]' % (indata.keyCode, hex(indata.keyCode))) |
| 541 | + | #print('Final keycode: %s' % hex(keycode)) |
| 542 | + | #print('Is pressed : %s' % indata.is_pressed) |
| 543 | + | #if keycode is not None: |
| 544 | + | # msg = pack("!BBxxI", 4, int(indata.is_pressed), keycode) |
| 545 | + | # self.__writer.write(msg) |
552 | 546 | | |
553 | 547 | | elif indata.type == RDPDATATYPE.KEYUNICODE: |
554 | 548 | | indata = cast(RDP_KEYBOARD_UNICODE, indata) |
| skipped 280 lines |