Python gửi tổ hợp phím linux

Kiểm soát hoàn toàn bàn phím của bạn với thư viện Python nhỏ này. Kết nối các sự kiện toàn cầu, đăng ký phím nóng, mô phỏng các lần nhấn phím và hơn thế nữa

Đặc trưng

  • Móc sự kiện toàn cầu trên tất cả các bàn phím [chụp các phím bất kể tiêu điểm]
  • Nghe và gửi các sự kiện bàn phím
  • Hoạt động với Windows và Linux [yêu cầu sudo], với hỗ trợ OS X thử nghiệm [cảm ơn @glitchassassin. ]
  • Python thuần túy, không có mô-đun C nào được biên dịch
  • Không phụ thuộc. Không quan trọng để cài đặt và triển khai, chỉ cần sao chép các tệp
  • Trăn 2 và 3
  • Hỗ trợ phím nóng phức tạp [e. g.
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    06] với thời gian chờ có thể kiểm soát
  • Bao gồm API cấp cao [e. g. và , ]
  • Các phím trên Bản đồ giống như thực tế trong bố cục của bạn, với sự hỗ trợ quốc tế hóa đầy đủ [e. g.
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    07]
  • Các sự kiện được ghi tự động trong chuỗi riêng biệt, không chặn chương trình chính
  • thử nghiệm và tài liệu
  • Không phá vỡ các phím chết có dấu [Tôi đang nhìn bạn, pyHook]
  • Hỗ trợ chuột có sẵn thông qua chuột dự án [
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    08]

Cách sử dụng

Cài đặt gói PyPI

pip install keyboard

hoặc sao chép kho lưu trữ [không cần cài đặt, tệp nguồn là đủ]

git clone //github.com/boppreh/keyboard

hoặc tải xuống và giải nén zip vào thư mục dự án của bạn

Sau đó kiểm tra để xem những tính năng có sẵn

Ví dụ

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]

hạn chế đã biết

  • Các sự kiện được tạo trong Windows không báo cáo id thiết bị [______409]. #21
  • Các khóa phương tiện trên Linux có thể xuất hiện không tên [chỉ mã quét] hoặc hoàn toàn không. #20
  • Ức chế/chặn phím chỉ khả dụng trên Windows. #22
  • Để tránh phụ thuộc vào X, các phần Linux đọc các tệp thiết bị thô [
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    10] nhưng điều này yêu cầu root
  • Các ứng dụng khác, chẳng hạn như một số trò chơi, có thể đăng ký móc nuốt tất cả các sự kiện quan trọng. Trong trường hợp này,
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    11 sẽ không thể báo cáo các sự kiện
  • Chương trình này không cố gắng ẩn mình, vì vậy đừng sử dụng nó cho keylogger hoặc bot trò chơi trực tuyến. Chịu trách nhiệm

Hãy nghĩ về tự động hóa GUI như lập trình một cánh tay robot. Bạn có thể lập trình cho cánh tay rô-bốt gõ bàn phím và di chuyển chuột cho bạn. Kỹ thuật này đặc biệt hữu ích cho các tác vụ liên quan đến việc nhấp chuột hoặc điền vào các biểu mẫu một cách vô thức.

Mô-đun

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
6 có các chức năng mô phỏng chuyển động của chuột, nhấp vào nút và cuộn con lăn chuột. Chương này chỉ đề cập đến một tập hợp con các tính năng của PyAutoGUI; . //pyautogui. đọcthedocs. tổ chức/ //pyautogui.readthedocs.org/ .

Cài đặt Mô-đun pyautogui

Mô-đun

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
6 có thể gửi các lần nhấn phím và nhấp chuột ảo tới Windows, OS X và Linux. Tùy thuộc vào hệ điều hành bạn đang sử dụng, bạn có thể phải cài đặt một số mô-đun khác [được gọi là các phần phụ thuộc ] trước khi có thể cài đặt PyAutoGUI.

  • Trên Windows, không có mô-đun nào khác để cài đặt

  • Trên OS X, hãy chạy

    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    8 ,
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    9
    , .
    >>> pyautogui.position[]
    [311, 622]
    >>> pyautogui.position[]
    [377, 481]
    >>> pyautogui.position[]
    [1536, 637]
    0
    .

  • Trên Linux, chạy

    >>> pyautogui.position[]
    [311, 622]
    >>> pyautogui.position[]
    [377, 481]
    >>> pyautogui.position[]
    [1536, 637]
    1 ,
    >>> pyautogui.position[]
    [311, 622]
    >>> pyautogui.position[]
    [377, 481]
    >>> pyautogui.position[]
    [1536, 637]
    2
    , < . [Scrot là một chương trình chụp màn hình mà PyAutoGUI sử dụng. ]
    >>> pyautogui.position[]
    [311, 622]
    >>> pyautogui.position[]
    [377, 481]
    >>> pyautogui.position[]
    [1536, 637]
    3
    , and
    >>> pyautogui.position[]
    [311, 622]
    >>> pyautogui.position[]
    [377, 481]
    >>> pyautogui.position[]
    [1536, 637]
    4
    . [Scrot is a screenshot program that PyAutoGUI uses.]

Sau khi các phần phụ thuộc này được cài đặt, hãy chạy

>>> pyautogui.position[]
[311, 622]
>>> pyautogui.position[]
[377, 481]
>>> pyautogui.position[]
[1536, 637]
5 [hoặc
>>> pyautogui.position[]
[311, 622]
>>> pyautogui.position[]
[377, 481]
>>> pyautogui.position[]
[1536, 637]
6
on OS X and Linux] to install PyAutoGUI.

Phụ lục A có thông tin đầy đủ về việc cài đặt các mô-đun của bên thứ ba. Để kiểm tra xem PyAutoGUI đã được cài đặt đúng chưa, hãy chạy

>>> pyautogui.position[]
[311, 622]
>>> pyautogui.position[]
[377, 481]
>>> pyautogui.position[]
[1536, 637]
7 từ trình bao tương tác và kiểm tra xem có bất kỳ thông báo lỗi nào không.

Đi đúng hướng

Trước khi chuyển sang tự động hóa GUI, bạn nên biết cách thoát khỏi các sự cố có thể phát sinh. Python có thể di chuyển chuột và gõ phím với tốc độ đáng kinh ngạc. Trên thực tế, có thể quá nhanh để các chương trình khác theo kịp. Ngoài ra, nếu có sự cố xảy ra nhưng chương trình của bạn vẫn tiếp tục di chuyển chuột, sẽ rất khó để biết chính xác chương trình đang làm gì hoặc cách khắc phục sự cố. Giống như những cây chổi bị phù phép trong bộ phim The Sorcerer's Apprentice của Disney, liên tục đổ đầy—và sau đó đổ đầy nước—vào bồn tắm của Mickey, chương trình của bạn có thể mất kiểm soát ngay cả khi . Việc dừng chương trình có thể khó khăn nếu con chuột tự di chuyển, khiến bạn không thể nhấp vào cửa sổ IDLE để đóng nó. May mắn thay, có một số cách để ngăn chặn hoặc phục hồi các sự cố tự động hóa GUI.

Tắt mọi thứ bằng cách đăng xuất

Có lẽ cách đơn giản nhất để dừng một chương trình tự động hóa GUI ngoài tầm kiểm soát là đăng xuất, thao tác này sẽ tắt tất cả các chương trình đang chạy. Trên Windows và Linux, phím nóng đăng xuất là CTRL - ALT -< . Trên OS X, đó là DEL. On OS X, it is

- SHIFT - TÙY CHỌN-Q. By logging out, you’ll lose any unsaved work, but at least you won’t have to wait for a full reboot of the computer.

Tạm dừng và không an toàn

Bạn có thể yêu cầu tập lệnh của mình đợi sau mỗi lệnh gọi hàm, cung cấp cho bạn một cửa sổ ngắn để kiểm soát chuột và bàn phím nếu có sự cố. Để thực hiện việc này, hãy đặt biến

>>> pyautogui.position[]
[311, 622]
>>> pyautogui.position[]
[377, 481]
>>> pyautogui.position[]
[1536, 637]
8 thành số giây bạn muốn tạm dừng. Ví dụ: sau khi cài đặt
>>> pyautogui.position[]
[311, 622]
>>> pyautogui.position[]
[377, 481]
>>> pyautogui.position[]
[1536, 637]
9, mọi lệnh gọi hàm PyAutoGUI sẽ đợi một giây rưỡi sau khi thực hiện hành động của nó. Hướng dẫn không phải PyAutoGUI sẽ không có tạm dừng này

PyAutoGUI cũng có tính năng fail-safe. Di chuyển con trỏ chuột đến góc trên bên trái của màn hình sẽ khiến PyAutoGUI tăng ngoại lệ

git clone //github.com/boppreh/keyboard
70. Chương trình của bạn có thể xử lý ngoại lệ này bằng các câu lệnh
git clone //github.com/boppreh/keyboard
71 và
git clone //github.com/boppreh/keyboard
72 hoặc để ngoại lệ đó làm hỏng chương trình của bạn. Dù bằng cách nào, tính năng fail-safe sẽ dừng chương trình nếu bạn nhanh chóng di chuyển chuột lên và sang trái hết mức có thể. Bạn có thể tắt tính năng này bằng cách cài đặt
git clone //github.com/boppreh/keyboard
73. Nhập thông tin sau vào vỏ tương tác

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
1

Ở đây, chúng tôi nhập

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
6 và đặt
>>> pyautogui.position[]
[311, 622]
>>> pyautogui.position[]
[377, 481]
>>> pyautogui.position[]
[1536, 637]
8 thành
git clone //github.com/boppreh/keyboard
76 để tạm dừng một giây sau mỗi lệnh gọi hàm. Chúng tôi đặt
git clone //github.com/boppreh/keyboard
77 thành
git clone //github.com/boppreh/keyboard
78 để bật tính năng không an toàn

Kiểm soát chuyển động của chuột

Trong phần này, bạn sẽ tìm hiểu cách di chuyển chuột và theo dõi vị trí của chuột trên màn hình bằng PyAutoGUI, nhưng trước tiên, bạn cần hiểu cách PyAutoGUI hoạt động với tọa độ

Các chức năng chuột của PyAutoGUI sử dụng tọa độ x và y. hiển thị hệ tọa độ cho màn hình máy tính; . Xuất xứ , trong đó xy are both zero, is at the upper-left corner of the screen. The x-coordinates increase going to the right, and the y-coordinates increase going down. All coordinates are positive integers; there are no negative coordinates.

Hình 18-1. Tọa độ của màn hình máy tính với độ phân giải 1920×1080

Độ phân giải resolution của bạn là màn hình của bạn có chiều rộng và chiều cao bao nhiêu pixel. Nếu độ phân giải màn hình của bạn được đặt thành 1920×1080, thì tọa độ của góc trên bên trái sẽ là [0, 0] và tọa độ của góc dưới bên phải sẽ là [1919, 1079].

Hàm

git clone //github.com/boppreh/keyboard
79 trả về một bộ hai số nguyên của chiều rộng và chiều cao của màn hình tính bằng pixel. Nhập thông tin sau vào vỏ tương tác

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
8

git clone //github.com/boppreh/keyboard
79 trả về
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
01 trên máy tính có độ phân giải 1920×1080; . Bạn có thể lưu trữ chiều rộng và chiều cao từ
git clone //github.com/boppreh/keyboard
79 trong các biến như
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
03 và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
04 để chương trình của bạn dễ đọc hơn

Di chuyển chuột

Bây giờ bạn đã hiểu tọa độ màn hình, hãy di chuyển chuột. Hàm

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
05 sẽ ngay lập tức di chuyển con trỏ chuột đến một vị trí xác định trên màn hình. Các giá trị nguyên cho tọa độ x và y lần lượt tạo nên các đối số thứ nhất và thứ hai của hàm. Đối số từ khóa float hoặc số nguyên
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
06 tùy chọn chỉ định số giây cần thiết để di chuyển chuột đến đích. Nếu bạn bỏ nó đi, mặc định là
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
07 cho chuyển động tức thời. [Tất cả các đối số từ khóa
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
06 trong hàm PyAutoGUI là tùy chọn. ] Nhập thông tin sau vào trình bao tương tác

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
8

Ví dụ này di chuyển con trỏ chuột theo chiều kim đồng hồ theo hình vuông giữa bốn tọa độ được cung cấp tổng cộng mười lần. Mỗi chuyển động mất một phần tư giây, như được chỉ định bởi đối số từ khóa

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
09. Nếu bạn chưa chuyển đối số thứ ba cho bất kỳ lệnh gọi
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
05 nào, thì con trỏ chuột sẽ dịch chuyển tức thời từ điểm này sang điểm khác

Hàm

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
11 di chuyển con trỏ chuột tương đối đến vị trí hiện tại của nó. Ví dụ sau di chuyển chuột theo cùng một mẫu hình vuông, ngoại trừ nó bắt đầu hình vuông từ bất cứ nơi nào chuột xuất hiện trên màn hình khi mã bắt đầu chạy.

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
11 cũng có ba đối số. bao nhiêu pixel để di chuyển theo chiều ngang sang bên phải, bao nhiêu pixel để di chuyển theo chiều dọc xuống dưới và [tùy chọn] mất bao lâu để hoàn thành chuyển động. Một số nguyên âm cho đối số thứ nhất hoặc thứ hai sẽ khiến chuột di chuyển sang trái hoặc lên trên tương ứng

Lấy vị trí chuột

Bạn có thể xác định vị trí hiện tại của chuột bằng cách gọi hàm

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
13, hàm này sẽ trả về một bộ giá trị của con trỏ chuột xy positions at the time of the function call. Enter the following into the interactive shell, moving the mouse around after each call:

>>> pyautogui.position[]
[311, 622]
>>> pyautogui.position[]
[377, 481]
>>> pyautogui.position[]
[1536, 637]

Tất nhiên, các giá trị trả về của bạn sẽ khác nhau tùy thuộc vào vị trí con trỏ chuột của bạn

Dự định. “Chuột đang ở đâu bây giờ?”

Khả năng xác định vị trí chuột là một phần quan trọng trong việc thiết lập tập lệnh tự động hóa GUI của bạn. Nhưng gần như không thể tìm ra tọa độ chính xác của pixel chỉ bằng cách nhìn vào màn hình. Sẽ rất hữu ích nếu có một chương trình liên tục hiển thị tọa độ x và y của con trỏ chuột khi bạn di chuyển nó xung quanh

Ở cấp độ cao, đây là những gì chương trình của bạn nên làm

  • Hiển thị tọa độ x và y hiện tại của con trỏ chuột

  • Cập nhật các tọa độ này khi chuột di chuyển quanh màn hình

Điều này có nghĩa là mã của bạn sẽ cần phải làm như sau

  • Gọi hàm

    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    14 để lấy tọa độ hiện tại

  • Xóa tọa độ đã in trước đó bằng cách in

    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    15 ký tự xóa lùi ra màn hình

  • Xử lý ngoại lệ

    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    16 để người dùng có thể nhấn CTRL -C để thoát.

Mở một cửa sổ chỉnh sửa tệp mới và lưu dưới dạng mouseNow. p .

Bước 1. Nhập mô-đun

Bắt đầu chương trình của bạn với những điều sau đây

git clone //github.com/boppreh/keyboard
7

Phần đầu của chương trình nhập mô-đun

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
6 và in lời nhắc cho người dùng rằng họ phải nhấn CTRL -C để thoát.

Bước 2. Thiết lập mã thoát và vòng lặp vô hạn

Bạn có thể sử dụng vòng lặp vô hạn

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
18 để liên tục in tọa độ chuột hiện tại từ
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
19. Đối với mã thoát khỏi chương trình, bạn sẽ cần nắm bắt ngoại lệ
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
16, ngoại lệ này xuất hiện bất cứ khi nào người dùng nhấn CTRL -C. Nếu bạn không xử lý ngoại lệ này, nó sẽ hiển thị một thông báo lỗi và truy nguyên xấu xí cho người dùng. Thêm phần sau vào chương trình của bạn.

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
0

Để xử lý ngoại lệ, hãy đặt vòng lặp vô hạn

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
18 trong câu lệnh
git clone //github.com/boppreh/keyboard
71. Khi người dùng nhấn CTRL -C, phần thực thi chương trình sẽ chuyển sang mệnh đề
git clone //github.com/boppreh/keyboard
72 ❶ và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
84 sẽ được in trên một dòng mới ❷.

Bước 3. Nhận và in tọa độ chuột

Mã bên trong vòng lặp

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
18 sẽ lấy tọa độ chuột hiện tại, định dạng chúng cho đẹp mắt và in chúng. Thêm đoạn mã sau vào bên trong vòng lặp
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
18

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
1

Sử dụng thủ thuật gán nhiều lần, các biến

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
87 và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
88 được cung cấp các giá trị của hai số nguyên được trả về trong bộ dữ liệu từ
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
13. Bằng cách chuyển ________ 587 và ________ 588 cho hàm ________ 672, bạn có thể nhận dạng chuỗi của tọa độ nguyên. Phương thức chuỗi
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
73 sẽ căn phải chúng để chúng chiếm cùng một lượng không gian, cho dù tọa độ có một, hai, ba hay bốn chữ số. Nối tọa độ chuỗi căn phải với nhãn
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
74 và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
75 sẽ cho chúng ta một chuỗi được định dạng gọn gàng, chuỗi này sẽ được lưu trữ trong
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
76

Vào cuối chương trình của bạn, thêm đoạn mã sau

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
8

Điều này thực sự in

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
76 ra màn hình. Đối số từ khóa
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
78 thành
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
79 ngăn ký tự dòng mới mặc định được thêm vào cuối dòng được in. Có thể xóa văn bản bạn đã in ra màn hình—nhưng chỉ đối với dòng văn bản gần đây nhất. Khi bạn in một ký tự xuống dòng, bạn không thể xóa bất kỳ thứ gì được in trước ký tự đó

Để xóa văn bản, hãy in ký tự thoát khoảng lùi

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
15. Ký tự đặc biệt này xóa một ký tự ở cuối dòng hiện tại trên màn hình. Dòng tại ❶ sử dụng sao chép chuỗi để tạo ra chuỗi có số lượng ký tự
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
15 bằng với độ dài của chuỗi được lưu trữ trong
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
76, điều này có tác dụng xóa chuỗi
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
76 được in lần cuối

Vì một lý do kỹ thuật nằm ngoài phạm vi của cuốn sách này, hãy luôn chuyển các cuộc gọi

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
804 đến
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
79 in
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
15 ký tự xóa lùi. Nếu không, màn hình có thể không cập nhật văn bản như mong muốn

Vì vòng lặp

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
18 lặp lại quá nhanh nên người dùng sẽ không thực sự nhận thấy rằng bạn đang xóa và in lại toàn bộ số trên màn hình. Ví dụ: nếu tọa độ x là 563 và chuột di chuyển một pixel sang bên phải, có vẻ như chỉ có 3 trong 563 được đổi thành 4

Khi bạn chạy chương trình, sẽ chỉ có hai dòng được in ra. Họ sẽ trông giống như một cái gì đó như thế này

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
7

Dòng đầu tiên hiển thị hướng dẫn nhấn CTRL -C để thoát. Dòng thứ hai có tọa độ chuột sẽ thay đổi khi bạn di chuyển chuột quanh màn hình. Sử dụng chương trình này, bạn sẽ có thể tìm ra tọa độ chuột cho các tập lệnh tự động hóa GUI của mình.

Kiểm soát tương tác chuột

Bây giờ bạn đã biết cách di chuyển chuột và tìm ra vị trí của chuột trên màn hình, bạn đã sẵn sàng để bắt đầu nhấp, kéo và cuộn

nhấp chuột

Để gửi một cú nhấp chuột ảo đến máy tính của bạn, hãy gọi phương thức

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
808. Theo mặc định, lần nhấp này sử dụng nút chuột trái và diễn ra ở bất kỳ vị trí nào của con trỏ chuột hiện tại. Bạn có thể chuyển tọa độ x và y của lần nhấp làm đối số thứ nhất và thứ hai tùy chọn nếu bạn muốn nó diễn ra ở một nơi khác ngoài vị trí hiện tại của chuột

Nếu bạn muốn chỉ định nút chuột nào sẽ sử dụng, hãy bao gồm đối số từ khóa

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
809, với giá trị là
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
810,
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
811 hoặc
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
812. Ví dụ:
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
813 sẽ nhấp chuột trái tại tọa độ [100, 150], trong khi
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
814 sẽ thực hiện nhấp chuột phải tại [200, 250]

Nhập thông tin sau vào vỏ tương tác

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
80

Bạn sẽ thấy con trỏ chuột di chuyển đến gần góc trên cùng bên trái của màn hình và nhấp một lần. Một "nhấp chuột" đầy đủ được định nghĩa là nhấn một nút chuột xuống và sau đó thả nó ra mà không di chuyển con trỏ. Bạn cũng có thể thực hiện nhấp chuột bằng cách gọi

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
815, chỉ nhấn nút chuột xuống và
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
816, chỉ nhả nút. Các hàm này có các đối số giống như
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
817 và trên thực tế, hàm
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
817 chỉ là một trình bao bọc thuận tiện xung quanh hai lệnh gọi hàm này

Để thuận tiện hơn, hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
819 sẽ thực hiện hai lần nhấp bằng nút chuột trái, trong khi hàm
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
820 và
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
821 sẽ thực hiện lần nhấp bằng nút chuột phải và chuột giữa tương ứng

Kéo chuột

Kéo có nghĩa là di chuyển chuột trong khi giữ một trong các nút chuột. Ví dụ: bạn có thể di chuyển tệp giữa các thư mục bằng cách kéo các biểu tượng thư mục hoặc bạn có thể di chuyển các cuộc hẹn trong ứng dụng lịch.

PyAutoGUI cung cấp các hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
822 và
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
823 để kéo con trỏ chuột đến vị trí mới hoặc vị trí tương ứng với vị trí hiện tại. Các đối số cho
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
824 và
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
825 cũng giống như
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
826 và
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
827. tọa độ x/chuyển động ngang, tọa độ y/chuyển động dọc và khoảng thời gian tùy chọn. [OS X không kéo chính xác khi chuột di chuyển quá nhanh, vì vậy nên chuyển đối số từ khóa
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
06. ]

Để thử các chức năng này, hãy mở ứng dụng vẽ đồ họa như Paint trên Windows, Paint Brush trên OS X hoặc GNU Paint trên Linux. [Nếu bạn không có ứng dụng vẽ, bạn có thể sử dụng ứng dụng trực tuyến tại http. // sumopaint. com/ . ] Tôi sẽ sử dụng PyAutoGUI để vẽ các ứng dụng này.

Khi con trỏ chuột trên canvas của ứng dụng vẽ và công cụ Pencil hoặc Brush được chọn, hãy nhập thông tin sau vào cửa sổ trình chỉnh sửa tệp mới và lưu dưới dạng spiralDraw. p .

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
81

Khi bạn chạy chương trình này, sẽ có độ trễ 5 giây ❶ để bạn di chuyển con trỏ chuột qua cửa sổ của chương trình vẽ bằng công cụ Pencil hoặc Brush được chọn. Sau đó, vẽ xoắn ốc. py sẽ kiểm soát chuột và nhấp chuột để đưa chương trình vẽ vào tiêu điểm ❷. Cửa sổ nằm trong tiêu điểm khi cửa sổ đó có con trỏ nhấp nháy đang hoạt động và các tác vụ bạn thực hiện—như nhập hoặc trong trường hợp này là kéo chuột—sẽ . Sau khi chương trình vẽ được lấy nét, spiralDraw. py vẽ một mẫu hình xoắn ốc hình vuông giống như trong.

Hình 18-2. Kết quả từ ví dụ

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
823

Biến

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
830 bắt đầu từ
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
831, vì vậy trong lần lặp đầu tiên của vòng lặp
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
18, lệnh gọi
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
825 đầu tiên sẽ kéo con trỏ sang bên phải 200 pixel, lấy 0. 2 giây ❸. Sau đó,
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
830 được giảm xuống 195 ❹ và lệnh gọi
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
825 thứ hai sẽ kéo con trỏ xuống 195 pixel ❺. Lệnh gọi
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
825 thứ ba kéo con trỏ –195 theo chiều ngang [195 sang trái] ❻,
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
830 được giảm xuống 190 và lệnh gọi
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
825 cuối cùng kéo con trỏ lên 190 pixel. Trên mỗi lần lặp, chuột được kéo sang phải, xuống, trái và lên và
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
830 nhỏ hơn một chút so với lần lặp trước. Bằng cách lặp lại mã này, bạn có thể di chuyển con trỏ chuột để vẽ một hình xoắn ốc vuông

Bạn có thể vẽ hình xoắn ốc này bằng tay [hay đúng hơn là bằng chuột], nhưng bạn phải thao tác chậm để chính xác như vậy. PyAutoGUI có thể làm điều đó trong vài giây

Ghi chú

Bạn có thể yêu cầu mã của mình vẽ hình ảnh bằng cách sử dụng các chức năng vẽ của

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
840 của mô-đun—xem Chương 17 để biết thêm thông tin. Nhưng việc sử dụng tính năng tự động hóa GUI cho phép bạn sử dụng các công cụ vẽ nâng cao mà các chương trình đồ họa có thể cung cấp, chẳng hạn như chuyển màu, các bút vẽ khác nhau hoặc hộp chứa đầy.

cuộn chuột

Chức năng cuối cùng của chuột PyAutoGUI là

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
841, mà bạn chuyển một đối số số nguyên cho số lượng đơn vị bạn muốn cuộn chuột lên hoặc xuống. Kích thước của một đơn vị khác nhau đối với từng hệ điều hành và ứng dụng, vì vậy bạn sẽ phải thử nghiệm để xem chính xác nó cuộn được bao xa trong tình huống cụ thể của bạn. Việc cuộn diễn ra tại vị trí hiện tại của con trỏ chuột. Truyền số nguyên dương cuộn lên và truyền số nguyên âm cuộn xuống. Chạy phần sau trong trình bao tương tác của IDLE trong khi con trỏ chuột ở trên cửa sổ IDLE

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
82

Bạn sẽ thấy IDLE cuộn nhanh lên trên—rồi quay xuống. Việc cuộn xuống xảy ra do IDLE tự động cuộn xuống dưới cùng sau khi thực hiện một lệnh. Nhập mã này để thay thế

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
83

Cái này nhập

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
842 và thiết lập một chuỗi trống,
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
843. Sau đó, mã lặp qua 200 số và thêm từng số vào
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
843, cùng với một dòng mới. Sau
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
845, khay nhớ tạm sẽ được tải với 200 dòng số. Mở cửa sổ soạn thảo tệp mới và dán văn bản vào đó. Điều này sẽ cung cấp cho bạn một cửa sổ văn bản lớn để thử cuộn vào. Nhập mã sau vào vỏ tương tác

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
84

Trên dòng thứ hai, bạn nhập hai lệnh được phân tách bằng dấu chấm phẩy, dấu chấm phẩy này cho phép Python chạy các lệnh như thể chúng nằm trên các dòng riêng biệt. Sự khác biệt duy nhất là trình bao tương tác sẽ không nhắc bạn nhập liệu giữa hai hướng dẫn. Điều này rất quan trọng đối với ví dụ này vì chúng tôi muốn cuộc gọi đến

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
846 diễn ra tự động sau khi chờ đợi. [Lưu ý rằng trong khi đặt hai lệnh trên một dòng có thể hữu ích trong trình bao tương tác, bạn vẫn nên đặt mỗi lệnh trên một dòng riêng biệt trong chương trình của mình. ]

Sau khi nhấn ENTER để chạy mã, bạn sẽ có năm giây để nhấp vào cửa sổ trình chỉnh sửa tệp để đặt nó vào tiêu điểm. Khi quá trình tạm dừng kết thúc, cuộc gọi

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
846 sẽ khiến cửa sổ trình chỉnh sửa tệp cuộn lên sau độ trễ năm giây.

Làm việc với màn hình

Các chương trình tự động hóa GUI của bạn không cần phải bấm và gõ mù quáng. PyAutoGUI có các tính năng chụp màn hình có thể tạo tệp hình ảnh dựa trên nội dung hiện tại của màn hình. Các chức năng này cũng có thể trả về một đối tượng Gối

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
848 xuất hiện trên màn hình hiện tại. Nếu bạn đã bỏ qua cuốn sách này, thì bạn nên đọc Chương 17 và cài đặt mô-đun
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
840 trước khi tiếp tục với phần này

Trên máy tính Linux, chương trình

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
850 cần được cài đặt để sử dụng chức năng chụp ảnh màn hình trong PyAutoGUI. Trong cửa sổ Terminal, hãy chạy
>>> pyautogui.position[]
[311, 622]
>>> pyautogui.position[]
[377, 481]
>>> pyautogui.position[]
[1536, 637]
2
để cài đặt chương trình này. Nếu bạn đang dùng Windows hoặc OS X, hãy bỏ qua bước này và tiếp tục với phần.

Chụp ảnh màn hình

Để chụp ảnh màn hình bằng Python, hãy gọi hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
852. Nhập thông tin sau vào vỏ tương tác

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
85

Biến

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
853 sẽ chứa đối tượng
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
848 của ảnh chụp màn hình. Bây giờ bạn có thể gọi các phương thức trên đối tượng
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
848 trong biến
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
853, giống như bất kỳ đối tượng
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
848 nào khác. Nhập thông tin sau vào vỏ tương tác

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
86

Vượt qua

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
858 một bộ tọa độ, chẳng hạn như [0, 0] hoặc [50, 200] và nó sẽ cho bạn biết màu của pixel tại các tọa độ đó trong hình ảnh của bạn. Giá trị trả về từ
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
858 là một bộ RGB gồm ba số nguyên cho lượng màu đỏ, lục và lam trong pixel. [Không có giá trị thứ tư cho alpha, vì ảnh chụp màn hình hoàn toàn mờ. ] Đây là cách chương trình của bạn có thể “nhìn thấy” những gì hiện có trên màn hình

Phân tích ảnh chụp màn hình

Giả sử rằng một trong các bước trong chương trình tự động hóa GUI của bạn là nhấp vào nút màu xám. Trước khi gọi phương thức

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
817, bạn có thể chụp ảnh màn hình và xem pixel nơi tập lệnh sắp nhấp vào. Nếu nó không có màu xám giống như nút màu xám, thì chương trình của bạn biết có gì đó không ổn. Có thể cửa sổ di chuyển bất ngờ hoặc có thể hộp thoại bật lên đã chặn nút. Tại thời điểm này, thay vì tiếp tục—và có thể gây ra sự tàn phá bằng cách bấm nhầm mục—chương trình của bạn có thể “thấy” rằng nó không bấm vào mục đúng và tự dừng lại

Hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
861 của PyAutoGUI sẽ trả về
git clone //github.com/boppreh/keyboard
78 nếu pixel ở tọa độ x và y đã cho trên màn hình khớp với màu đã cho. Đối số thứ nhất và thứ hai là số nguyên cho tọa độ x và y và đối số thứ ba là bộ ba số nguyên cho màu RGB mà pixel màn hình phải khớp. Nhập thông tin sau vào vỏ tương tác

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
87

Sau khi chụp ảnh màn hình và sử dụng

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
858 để lấy bộ RGB cho màu của pixel ở tọa độ cụ thể ❶, hãy chuyển cùng tọa độ và bộ RGB cho
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
861 ❷, bộ này sẽ trả về
git clone //github.com/boppreh/keyboard
78. Sau đó, thay đổi giá trị trong bộ RGB và gọi lại
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
861 cho cùng tọa độ ❸. Điều này sẽ trả về
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
867. Phương pháp này có thể hữu ích để gọi bất cứ khi nào chương trình tự động hóa GUI của bạn chuẩn bị gọi
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
817. Lưu ý rằng màu tại tọa độ đã cho phải chính xác khớp. Nếu nó hơi khác một chút—ví dụ:
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
869 thay vì
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
870—thì
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
861 sẽ trả về
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
872.

Dự định. Mở rộng chương trình mouseNow

Bạn có thể mở rộng mouseNow. py từ đầu chương này để nó không chỉ cung cấp tọa độ x và y của vị trí hiện tại của con trỏ chuột mà còn cung cấp màu RGB của pixel bên dưới con trỏ. Sửa đổi mã bên trong vòng lặp

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
18 của mouseNow. py trông như thế này.

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
88

Bây giờ, khi bạn chạy mouseNow. py , đầu ra sẽ bao gồm giá trị màu RGB của pixel dưới con trỏ chuột.

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
89

Thông tin này, cùng với chức năng

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
861, sẽ giúp dễ dàng thêm kiểm tra màu pixel vào tập lệnh tự động hóa GUI của bạn

Nhận dạng hình ảnh

Nhưng nếu bạn không biết trước PyAutoGUI nên nhấp vào đâu thì sao? . Cung cấp cho PyAutoGUI một hình ảnh về những gì bạn muốn nhấp và để nó tìm ra tọa độ

Ví dụ: nếu trước đó bạn đã chụp ảnh màn hình để chụp ảnh nút Gửi trong gửi. png , hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
875 sẽ trả về tọa độ nơi hình ảnh đó được tìm thấy. Để xem cách thức hoạt động của
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
875, hãy thử chụp ảnh màn hình một vùng nhỏ trên màn hình của bạn; .

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
80

Bộ bốn số nguyên mà

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
875 trả về có tọa độ x của cạnh trái, tọa độ y của cạnh trên, chiều rộng và chiều cao của vị trí đầu tiên trên màn hình mà hình ảnh được tìm thấy. Nếu bạn đang thử điều này trên máy tính bằng ảnh chụp màn hình của riêng mình, thì giá trị trả về của bạn sẽ khác với giá trị hiển thị ở đây

Nếu không tìm thấy hình ảnh trên màn hình,

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
875 sẽ trả về
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
880. Lưu ý rằng hình ảnh trên màn hình phải hoàn toàn khớp với hình ảnh được cung cấp để được nhận dạng. Nếu hình ảnh bị tắt thậm chí một pixel,
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
875 sẽ trả về
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
880

Nếu hình ảnh có thể được tìm thấy ở một vài nơi trên màn hình, thì

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
883 sẽ trả về một đối tượng
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
884, đối tượng này có thể được chuyển đến
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
885 để trả về một danh sách các bộ bốn số nguyên. Sẽ có một bộ bốn số nguyên cho mỗi vị trí tìm thấy hình ảnh trên màn hình. Tiếp tục ví dụ về shell tương tác bằng cách nhập thông tin sau [và thay thế
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
886 bằng tên tệp hình ảnh của riêng bạn]

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
81

Mỗi bộ bốn số nguyên đại diện cho một vùng trên màn hình. Nếu hình ảnh của bạn chỉ được tìm thấy trong một khu vực, thì việc sử dụng

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
885 và
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
883 chỉ trả về một danh sách chứa một bộ

Sau khi bạn có bộ dữ liệu bốn số nguyên cho khu vực trên màn hình nơi hình ảnh của bạn được tìm thấy, bạn có thể nhấp vào tâm của khu vực này bằng cách chuyển bộ dữ liệu đó cho hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
889 để trả về tọa độ x và y của trung tâm khu vực. Nhập thông tin sau vào trình bao tương tác, thay thế các đối số bằng tên tệp, bộ bốn số nguyên và cặp tọa độ của riêng bạn

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
82

Sau khi bạn có tọa độ trung tâm từ

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
889, việc chuyển tọa độ đến
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
817 sẽ nhấp vào tâm của khu vực trên màn hình khớp với hình ảnh bạn đã chuyển đến
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
875

Điều khiển bàn phím

PyAutoGUI cũng có các chức năng gửi các lần nhấn phím ảo đến máy tính của bạn, cho phép bạn điền vào các biểu mẫu hoặc nhập văn bản vào các ứng dụng

Gửi một chuỗi từ bàn phím

Hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
893 gửi các lần nhấn phím ảo đến máy tính. Tác dụng của các lần nhấn phím này phụ thuộc vào cửa sổ và trường văn bản nào có tiêu điểm. Trước tiên, bạn có thể muốn gửi một cú nhấp chuột đến trường văn bản bạn muốn để đảm bảo rằng nó có tiêu điểm

Ví dụ đơn giản, hãy sử dụng Python để tự động nhập các từ Xin chào thế giới. vào cửa sổ chỉnh sửa tệp. Trước tiên, hãy mở cửa sổ trình chỉnh sửa tệp mới và đặt nó ở góc trên bên trái màn hình của bạn để PyAutoGUI sẽ nhấp vào đúng vị trí để đưa nó vào tiêu điểm. Tiếp theo, nhập thông tin sau vào trình bao tương tác.

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
83

Lưu ý cách đặt hai lệnh trên cùng một dòng, được phân tách bằng dấu chấm phẩy, ngăn trình bao tương tác nhắc bạn nhập liệu giữa khi chạy hai lệnh. Điều này ngăn bạn vô tình đưa một cửa sổ mới vào tiêu điểm giữa các cuộc gọi

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
817 và
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895, điều này sẽ làm rối ví dụ

Python trước tiên sẽ gửi một lần nhấp chuột ảo đến tọa độ [100, 100], thao tác này sẽ nhấp vào cửa sổ trình chỉnh sửa tệp và đặt nó vào tiêu điểm. Cuộc gọi

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895 sẽ gửi văn bản Xin chào thế giới. vào cửa sổ, làm cho nó giống như. Bây giờ bạn có mã có thể gõ cho bạn.

Hình 18-3. Sử dụng PyAutogGUI để nhấp vào cửa sổ chỉnh sửa tệp và nhập Xin chào thế giới. vào đó

Theo mặc định, hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895 sẽ nhập chuỗi đầy đủ ngay lập tức. Tuy nhiên, bạn có thể chuyển đối số thứ hai tùy chọn để thêm khoảng dừng ngắn giữa mỗi ký tự. Đối số thứ hai này là một số nguyên hoặc giá trị float của số giây để tạm dừng. Ví dụ:
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
898 sẽ đợi một phần tư giây sau khi nhập H , một phần tư giây nữa sau khi nhập e, and so on. This gradual typewriter effect may be useful for slower applications that can’t process keystrokes fast enough to keep up with PyAutoGUI.

Đối với các ký tự như A hoặc . , PyAutoGUI cũng sẽ tự động mô phỏng việc giữ phím SHIFT .

tên chính

Không phải tất cả các phím đều dễ biểu diễn bằng các ký tự văn bản đơn lẻ. Ví dụ: làm cách nào để bạn biểu thị SHIFT hoặc phím mũi tên trái dưới dạng một ký tự? .

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
899 cho phím ESC hoặc
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
800 cho phím ENTER .

Thay vì một đối số chuỗi đơn, danh sách các chuỗi phím bàn phím này có thể được chuyển đến

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895. Ví dụ cuộc gọi sau nhấn phím A, sau đó nhấn phím B, sau đó nhấn phím mũi tên trái hai lần và cuối cùng là phím X và Y

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
84

Bởi vì việc nhấn phím mũi tên trái sẽ di chuyển con trỏ bàn phím, thao tác này sẽ xuất ra XYab . liệt kê các chuỗi phím bàn phím PyAutoGUI mà bạn có thể chuyển đến

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895 để mô phỏng cách nhấn bất kỳ tổ hợp phím nào.

Bạn cũng có thể kiểm tra danh sách

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
803 để xem tất cả các chuỗi phím bàn phím có thể có mà PyAutoGUI sẽ chấp nhận. Chuỗi
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
804 đề cập đến phím SHIFT bên trái và tương đương với
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
805. Điều tương tự cũng áp dụng cho các chuỗi
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
806,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
807 và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
808; .

Bảng 18-1. Thuộc tính

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
809

Chuỗi phím bàn phím

Nghĩa

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
810,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
811,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
812,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
813,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
814,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
815,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
816,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
817,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
818,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
819,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
820,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
821, v.v.

Các phím cho các ký tự đơn

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
800 [hoặc
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
823 hoặc
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
824]

Phím ENTER key

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
899

Phím ESC key

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
805,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
827

Các phím SHIFT trái và phải

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
828,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
829

Phím trái và phải ALT keys

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
830,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
831

Phím trái và phải CTRL keys

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
832 [hoặc
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
833]

Phím TAB key

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
834,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
835

Các phím BACKSPACEDELETE keys

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
836,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
837

Các phím PAGE UPPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWNPAGE DOWN keys

________ 3838, ________ 3839

Các phím HOMEEND keys

________ 3840, ________ 3841, ________ 2810, ________ 2812

Các phím mũi tên lên, xuống, trái, phải

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
844,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
845,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
846, v.v.

Các phím F1 đến F12

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
847,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
848,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
849

Các phím tắt tiếng, giảm âm lượng và tăng âm lượng [một số bàn phím không có các phím này nhưng hệ điều hành của bạn vẫn có thể hiểu được các lần nhấn phím mô phỏng này]

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
850

Phím TẠM DỪNG key

________ 3851, ________ 3852, ________ 3853

The CAPS LOCK , NUM LOCK và < SCROLL LOCK keys

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
854

Phím INS hoặc INSERT key

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
855

Phím PRTSC hoặc PRINT SCREEN key

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
856,
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
857

Phím trái và phải WIN [trên Windows]

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
858

Phím Command [

] [trên OS X]
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
859 Phím OPTION [trên OS X

Nhấn và nhả bàn phím

Giống như các hàm

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
860 và ________ 3861, _______ 3862 và ________ 3863 sẽ gửi các lần nhấn và nhả phím ảo đến máy tính. Họ được thông qua một chuỗi phím bàn phím [xem ] cho đối số của họ. Để thuận tiện, PyAutoGUI cung cấp hàm
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
864 gọi cả hai hàm này để mô phỏng một lần nhấn phím hoàn chỉnh

Chạy mã sau, mã này sẽ nhập ký tự ký hiệu đô la [có được bằng cách giữ phím SHIFT và nhấn 4].

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
85

Dòng này nhấn xuống SHIFT , nhấn [và nhả] 4, sau đó nhả SHIFT< . Nếu bạn cần nhập một chuỗi vào trường văn bản, hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895 sẽ phù hợp hơn. Nhưng đối với các ứng dụng sử dụng lệnh một phím, hàm
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
866 là cách tiếp cận đơn giản hơn. . If you need to type a string into a text field, the
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895 function is more suitable. But for applications that take single-key commands, the
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
866 function is the simpler approach.

Tổ hợp phím nóng

A phím nóng hoặc phím tắt là sự kết hợp của các lần nhấn phím . Phím nóng phổ biến để sao chép lựa chọn là CTRL -C [trên Windows và Linux] hoặc ⌘-C [trên OS X]. Người dùng nhấn và giữ phím CTRL , sau đó nhấn phím C, sau đó nhả phím C và CTRL< . Để thực hiện việc này với các hàm

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
867 và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
868 của PyAutoGUI, bạn sẽ phải nhập thông tin sau. keys. To do this with PyAutoGUI’s
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
867 and
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
868 functions, you would have to enter the following:

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
86

Việc này khá phức tạp. Thay vào đó, hãy sử dụng hàm

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
869, hàm này nhận nhiều đối số chuỗi phím trên bàn phím, nhấn chúng theo thứ tự và nhả chúng theo thứ tự ngược lại. Đối với ví dụ CTRL -C, mã sẽ đơn giản như sau.

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
87

Chức năng này đặc biệt hữu ích cho các tổ hợp phím nóng lớn hơn. Trong Word, CTRL - ALT - SHIFT-S hotkey combination displays the Style pane. Instead of making eight different function calls [four

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
867 calls and four
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
868 calls], you can just call
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
872.

Với cửa sổ trình chỉnh sửa tệp IDLE mới ở góc trên bên trái màn hình của bạn, hãy nhập thông tin sau vào trình bao tương tác [trong OS X, thay thế

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
807 bằng
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
806]

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
88

Điều này xác định một hàm

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
875, khi được gọi, sẽ nhấp vào cửa sổ trình chỉnh sửa tệp để đưa nó vào tiêu điểm ❶, nhập Trong IDLE, Atl-3 nhận xét ra một dòng ❷, pause for 2 seconds, and then simulate pressing the ALT-3 hotkey [or CTRL-3 on OS X] ❸. This keyboard shortcut adds two # vào dòng hiện tại, nhận xét dòng đó. [Đây là một thủ thuật hữu ích cần biết khi viết mã của riêng bạn trong IDLE. ]

Xem xét các chức năng PyAutoGUI

Vì chương này đề cập đến nhiều chức năng khác nhau nên đây là tài liệu tham khảo tóm tắt nhanh

  • ______3876

    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    87
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    88
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880. Moves the mouse cursor to the given ____ 587____ 588 đã cho.

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    883 ______3884
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    886
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880. Moves the mouse cursor relative to its current position.

  • ______3888

    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    87
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    88
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880. Moves the mouse cursor while the left button is held down.

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    893 ______3884
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    886
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880. Moves the mouse cursor relative to its current position while the left button is held down.

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    898
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    87
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    88
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    import keyboard
    
    keyboard.press_and_release['shift+s, space']
    
    keyboard.write['The quick brown fox jumps over the lazy dog.']
    
    keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]
    
    # Press PAGE UP then PAGE DOWN to type "foobar".
    keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]
    
    # Blocks until you press esc.
    keyboard.wait['esc']
    
    # Record events until 'esc' is pressed.
    recorded = keyboard.record[until='esc']
    # Then replay back at three times the speed.
    keyboard.play[recorded, speed_factor=3]
    
    # Type @@ then press space to replace with abbreviation.
    keyboard.add_abbreviation['@@', 'my.long.email@example.com']
    
    # Block forever, like `while True`.
    keyboard.wait[]
    
    809
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880. Simulates a click [left button by default].

  • ______305 . Mô phỏng thao tác nhấp chuột phải.

  • ______306 . Mô phỏng thao tác bấm vào nút giữa.

  • ______307 . Mô phỏng thao tác nhấp đúp vào nút trái.

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    08
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    87
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    88
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    import keyboard
    
    keyboard.press_and_release['shift+s, space']
    
    keyboard.write['The quick brown fox jumps over the lazy dog.']
    
    keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]
    
    # Press PAGE UP then PAGE DOWN to type "foobar".
    keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]
    
    # Blocks until you press esc.
    keyboard.wait['esc']
    
    # Record events until 'esc' is pressed.
    recorded = keyboard.record[until='esc']
    # Then replay back at three times the speed.
    keyboard.play[recorded, speed_factor=3]
    
    # Type @@ then press space to replace with abbreviation.
    keyboard.add_abbreviation['@@', 'my.long.email@example.com']
    
    # Block forever, like `while True`.
    keyboard.wait[]
    
    809
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880. Simulates pressing down the given button at the position
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    87
    ,
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    88
    .

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    17
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    87
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    88
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    import keyboard
    
    keyboard.press_and_release['shift+s, space']
    
    keyboard.write['The quick brown fox jumps over the lazy dog.']
    
    keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]
    
    # Press PAGE UP then PAGE DOWN to type "foobar".
    keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]
    
    # Blocks until you press esc.
    keyboard.wait['esc']
    
    # Record events until 'esc' is pressed.
    recorded = keyboard.record[until='esc']
    # Then replay back at three times the speed.
    keyboard.play[recorded, speed_factor=3]
    
    # Type @@ then press space to replace with abbreviation.
    keyboard.add_abbreviation['@@', 'my.long.email@example.com']
    
    # Block forever, like `while True`.
    keyboard.wait[]
    
    809
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880. Simulates releasing the given button at the position
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    87
    ,
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    88
    .

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    26
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    27
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880 . Mô phỏng bánh xe cuộn. Một đối số tích cực cuộn lên; .

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    29
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    30
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880 . Nhập các ký tự trong chuỗi thông báo đã cho.

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    32
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    33
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    35
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    37
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    38. Types the given keyboard key strings.

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    39
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    40
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880 . Nhấn chuỗi phím bàn phím đã cho.

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    42
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    40
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880 . Mô phỏng nhấn phím bàn phím đã cho.

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    45
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    40
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    880 . Mô phỏng nhả phím bàn phím đã cho.

  • >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    48
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    33
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    35
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    878
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    37
    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    38. Simulates pressing the given keyboard key strings down in order and then releasing them in reverse order.

  • ______355 . Trả về ảnh chụp màn hình dưới dạng đối tượng

    import keyboard
    
    keyboard.press_and_release['shift+s, space']
    
    keyboard.write['The quick brown fox jumps over the lazy dog.']
    
    keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]
    
    # Press PAGE UP then PAGE DOWN to type "foobar".
    keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]
    
    # Blocks until you press esc.
    keyboard.wait['esc']
    
    # Record events until 'esc' is pressed.
    recorded = keyboard.record[until='esc']
    # Then replay back at three times the speed.
    keyboard.play[recorded, speed_factor=3]
    
    # Type @@ then press space to replace with abbreviation.
    keyboard.add_abbreviation['@@', 'my.long.email@example.com']
    
    # Block forever, like `while True`.
    keyboard.wait[]
    
    848. [Xem Chương 17 để biết thông tin về đối tượng
    import keyboard
    
    keyboard.press_and_release['shift+s, space']
    
    keyboard.write['The quick brown fox jumps over the lazy dog.']
    
    keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]
    
    # Press PAGE UP then PAGE DOWN to type "foobar".
    keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]
    
    # Blocks until you press esc.
    keyboard.wait['esc']
    
    # Record events until 'esc' is pressed.
    recorded = keyboard.record[until='esc']
    # Then replay back at three times the speed.
    keyboard.play[recorded, speed_factor=3]
    
    # Type @@ then press space to replace with abbreviation.
    keyboard.add_abbreviation['@@', 'my.long.email@example.com']
    
    # Block forever, like `while True`.
    keyboard.wait[]
    
    848. ]

Dự định. Trình điền biểu mẫu tự động

Trong tất cả những công việc nhàm chán, điền vào biểu mẫu là công việc đáng sợ nhất. Nó chỉ phù hợp rằng bây giờ, trong dự án chương cuối cùng, bạn sẽ giết nó. Giả sử bạn có một lượng dữ liệu khổng lồ trong một bảng tính và bạn phải gõ lại dữ liệu đó một cách tẻ nhạt vào giao diện biểu mẫu của một số ứng dụng khác—không có nhân viên thực tập nào làm việc đó cho bạn. Mặc dù một số ứng dụng sẽ có tính năng Nhập cho phép bạn tải lên bảng tính có thông tin, nhưng đôi khi có vẻ như không có cách nào khác ngoài việc nhấp và gõ hàng giờ liên tục. Bạn đã đi xa đến thế trong cuốn sách này; . of course there’s another way.

Biểu mẫu cho dự án này là biểu mẫu Google Tài liệu mà bạn có thể tìm thấy tại http. //tác giả. com/form . Nó trông giống như.

Hình 18-4. Biểu mẫu được sử dụng cho dự án này

Ở cấp độ cao, đây là những gì chương trình của bạn nên làm

  • Nhấp vào trường văn bản đầu tiên của biểu mẫu

  • Di chuyển qua biểu mẫu, nhập thông tin vào từng trường

  • Nhấp vào nút Gửi

  • Lặp lại quy trình với bộ dữ liệu tiếp theo

Điều này có nghĩa là mã của bạn sẽ cần phải làm như sau

  • Gọi

    import keyboard
    
    keyboard.press_and_release['shift+s, space']
    
    keyboard.write['The quick brown fox jumps over the lazy dog.']
    
    keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]
    
    # Press PAGE UP then PAGE DOWN to type "foobar".
    keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]
    
    # Blocks until you press esc.
    keyboard.wait['esc']
    
    # Record events until 'esc' is pressed.
    recorded = keyboard.record[until='esc']
    # Then replay back at three times the speed.
    keyboard.play[recorded, speed_factor=3]
    
    # Type @@ then press space to replace with abbreviation.
    keyboard.add_abbreviation['@@', 'my.long.email@example.com']
    
    # Block forever, like `while True`.
    keyboard.wait[]
    
    808 để nhấp vào biểu mẫu và nút Gửi

  • Gọi

    import keyboard
    
    keyboard.press_and_release['shift+s, space']
    
    keyboard.write['The quick brown fox jumps over the lazy dog.']
    
    keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]
    
    # Press PAGE UP then PAGE DOWN to type "foobar".
    keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]
    
    # Blocks until you press esc.
    keyboard.wait['esc']
    
    # Record events until 'esc' is pressed.
    recorded = keyboard.record[until='esc']
    # Then replay back at three times the speed.
    keyboard.play[recorded, speed_factor=3]
    
    # Type @@ then press space to replace with abbreviation.
    keyboard.add_abbreviation['@@', 'my.long.email@example.com']
    
    # Block forever, like `while True`.
    keyboard.wait[]
    
    893 để nhập văn bản vào các trường

  • Xử lý ngoại lệ

    >>> import pyautogui
    >>> for i in range[10]:
          pyautogui.moveRel[100, 0, duration=0.25]
          pyautogui.moveRel[0, 100, duration=0.25]
          pyautogui.moveRel[-100, 0, duration=0.25]
          pyautogui.moveRel[0, -100, duration=0.25]
    16 để người dùng có thể nhấn CTRL -C để thoát.

Mở cửa sổ soạn thảo tệp mới và lưu dưới dạng formFiller. p .

Bước 1. Tìm hiểu các bước

Trước khi viết mã, bạn cần tìm ra chính xác các lần nhấn phím và nhấp chuột sẽ điền vào biểu mẫu một lần. mouseNow. py có thể giúp bạn tìm ra tọa độ chuột cụ thể. Bạn chỉ cần biết tọa độ của trường văn bản đầu tiên. Sau khi nhấp vào trường đầu tiên, bạn chỉ cần nhấn TAB để chuyển tiêu điểm sang trường tiếp theo. Điều này sẽ giúp bạn không phải tìm ra tọa độ x và y để nhấp cho mọi trường.

Dưới đây là các bước nhập dữ liệu vào biểu mẫu

  1. Nhấp vào trường Tên. [Sử dụng mouseNow. py để xác định tọa độ sau khi tối đa hóa cửa sổ trình duyệt. Trên OS X, bạn có thể cần nhấp hai lần. một lần để đặt tiêu điểm của trình duyệt và một lần nữa để nhấp vào trường Tên. ]

  2. Nhập tên rồi nhấn TAB .

  3. Nhập nỗi sợ lớn nhất rồi nhấn TAB .

  4. Nhấn phím mũi tên xuống đúng số lần để chọn nguồn điện của thuật sĩ. một lần cho cây đũa phép , hai lần cho bùa hộ mệnh , ba lần cho crystal ball, and four times for money. Then press TAB . [Lưu ý rằng trên OS X, bạn sẽ phải nhấn phím mũi tên xuống một lần nữa cho mỗi tùy chọn. Đối với một số trình duyệt, bạn có thể cần phải nhấn cả phím ENTER . ]

  5. Nhấn phím mũi tên phải để chọn câu trả lời cho câu hỏi Robocop. Nhấn một lần cho 2 , hai lần cho 3 , ba lần cho < . Sau đó nhấn 4, or four times for 5; or just press the spacebar to select 1 [which is highlighted by default]. Then press TAB .

  6. Nhập nhận xét bổ sung rồi nhấn TAB .

  7. Nhấn phím ENTER để “nhấp” vào nút Gửi.

  8. Sau khi gửi biểu mẫu, trình duyệt sẽ đưa bạn đến một trang nơi bạn cần nhấp vào liên kết để quay lại trang biểu mẫu

Lưu ý rằng nếu bạn chạy lại chương trình này sau đó, bạn có thể phải cập nhật tọa độ nhấp chuột, vì cửa sổ trình duyệt có thể đã thay đổi vị trí. Để giải quyết vấn đề này, hãy luôn đảm bảo cửa sổ trình duyệt được phóng to trước khi tìm tọa độ của trường biểu mẫu đầu tiên. Ngoài ra, các trình duyệt khác nhau trên các hệ điều hành khác nhau có thể hoạt động hơi khác so với các bước được đưa ra ở đây, vì vậy hãy kiểm tra xem các tổ hợp phím này có hoạt động với máy tính của bạn không trước khi chạy chương trình của bạn

Bước 2. Thiết lập tọa độ

Tải biểu mẫu mẫu mà bạn đã tải xuống [] trong trình duyệt và phóng to cửa sổ trình duyệt của bạn. Mở Terminal mới hoặc cửa sổ dòng lệnh để chạy mouseNow. py , sau đó di chuột qua trường Tên để tìm tọa độ x và y của nó. Những con số này sẽ được gán cho biến

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
61 trong chương trình của bạn. Ngoài ra, hãy tìm hiểu tọa độ x và y và giá trị bộ RGB của nút Gửi màu xanh lam. Các giá trị này sẽ được gán cho các biến
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
62 và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
63 tương ứng.

Tiếp theo, hãy điền một số dữ liệu giả cho biểu mẫu và nhấp vào Gửi . Bạn cần xem trang tiếp theo trông như thế nào để có thể sử dụng mouseNow. py để tìm tọa độ của liên kết Gửi phản hồi khác trên trang mới này.

Làm cho mã nguồn của bạn trông giống như sau, đảm bảo thay thế tất cả các giá trị in nghiêng bằng tọa độ bạn đã xác định từ các thử nghiệm của riêng mình

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
89

Bây giờ bạn cần dữ liệu mà bạn thực sự muốn nhập vào biểu mẫu này. Trong thế giới thực, dữ liệu này có thể đến từ bảng tính, tệp văn bản gốc hoặc trang web và nó sẽ yêu cầu mã bổ sung để tải vào chương trình. Nhưng đối với dự án này, bạn sẽ chỉ cần mã hóa cứng tất cả dữ liệu này trong một biến. Thêm phần sau vào chương trình của bạn

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
0

Danh sách

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
64 chứa bốn từ điển cho bốn tên khác nhau. Mỗi từ điển có tên của các trường văn bản dưới dạng khóa và phản hồi dưới dạng giá trị. Phần thiết lập cuối cùng là đặt biến PyAutoGUI ________ 365 đợi nửa giây sau mỗi lệnh gọi hàm. Thêm phần sau vào chương trình của bạn sau câu lệnh gán
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
64

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
1

Bước 3. Bắt đầu nhập dữ liệu

Một vòng lặp

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
67 sẽ lặp qua từng từ điển trong danh sách
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
64, chuyển các giá trị trong từ điển tới các hàm PyAutoGUI hầu như sẽ nhập vào các trường văn bản

Thêm đoạn mã sau vào chương trình của bạn

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
2

Là một tính năng an toàn nhỏ, tập lệnh có khoảng dừng năm giây ❶ để người dùng có cơ hội nhấn CTRL -C [ . Sau đó, chương trình đợi cho đến khi màu của nút Gửi hiển thị ❷, cho chương trình biết rằng trang biểu mẫu đã được tải. Hãy nhớ rằng bạn đã tìm ra thông tin tọa độ và màu sắc ở bước 2 và lưu trữ nó trong các biến

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
62 và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
63. Để sử dụng
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
861, bạn chuyển tọa độ
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
73 và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
74 và màu
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
63.

Sau khi mã đợi cho đến khi màu của nút Gửi hiển thị, hãy thêm đoạn mã sau

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
3

Chúng tôi thỉnh thoảng thêm một lệnh gọi

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
79 để hiển thị trạng thái của chương trình trong cửa sổ Terminal để cho người dùng biết chuyện gì đang xảy ra ❶

Vì chương trình biết rằng biểu mẫu đã được tải, nên đã đến lúc gọi

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
817 để nhấp vào trường Tên ❷ và
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895 để nhập chuỗi vào
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
79 ❸. Ký tự
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
833 được thêm vào cuối chuỗi được chuyển đến
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895 để mô phỏng cách nhấn TAB , thao tác này sẽ di chuyển tiêu điểm bàn phím sang trường tiếp theo, Greatest Fear[ . Một cuộc gọi khác đến
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895 sẽ nhập chuỗi trong
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
83 vào trường này và sau đó chuyển sang trường tiếp theo ở dạng ❹.

Bước 4. Xử lý danh sách chọn và nút radio

Trình đơn thả xuống cho câu hỏi “sức mạnh của thuật sĩ” và các nút radio cho trường Robocop khó xử lý hơn so với trường văn bản. Để nhấp vào các tùy chọn này bằng chuột, bạn sẽ phải tìm ra tọa độ x và y của từng tùy chọn có thể. Thay vào đó, việc sử dụng các phím mũi tên trên bàn phím để thực hiện lựa chọn sẽ dễ dàng hơn

Thêm phần sau vào chương trình của bạn

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
4

Sau khi menu thả xuống có tiêu điểm [hãy nhớ rằng bạn đã viết mã để mô phỏng việc nhấn TAB sau khi điền vào [các] Nỗi sợ hãi lớn nhất . Tùy thuộc vào giá trị trong

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
84, chương trình của bạn sẽ gửi một số lần nhấn phím mũi tên xuống trước khi chuyển sang trường tiếp theo. Nếu giá trị tại phím
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
85 trong từ điển của người dùng này là
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
86 ❶, thì chúng tôi mô phỏng việc nhấn phím mũi tên xuống một lần [để chọn Wand ] và nhấn TAB ❷. If the value at the
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
85 key is
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
88, we simulate pressing the down arrow key twice and pressing TAB , v.v.

Có thể chọn các nút radio cho câu hỏi Robocop bằng các phím mũi tên bên phải—hoặc, nếu bạn muốn chọn lựa chọn đầu tiên ❸, chỉ bằng cách nhấn phím cách ❹

Bước 5. Gửi biểu mẫu và chờ đợi

Bạn có thể điền vào trường Nhận xét bổ sung bằng hàm

import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
895 bằng cách chuyển
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
90 làm đối số. Bạn có thể gõ thêm
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
833 để di chuyển tiêu điểm bàn phím sang trường tiếp theo hoặc nút Gửi. Sau khi tiêu điểm nút Gửi, việc gọi
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
92 sẽ mô phỏng việc nhấn phím ENTER và gửi biểu mẫu. Sau khi gửi biểu mẫu, chương trình của bạn sẽ đợi năm giây để tải trang tiếp theo.

Sau khi trang mới được tải, nó sẽ có một Gửi phản hồi khác liên kết sẽ hướng trình duyệt đến một trang biểu mẫu trống mới. Bạn đã lưu tọa độ của liên kết này dưới dạng một bộ trong

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
93 ở bước 2, vì vậy hãy chuyển các tọa độ này cho
import keyboard

keyboard.press_and_release['shift+s, space']

keyboard.write['The quick brown fox jumps over the lazy dog.']

keyboard.add_hotkey['ctrl+shift+a', print, args=['triggered', 'hotkey']]

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey['page up, page down', lambda: keyboard.write['foobar']]

# Blocks until you press esc.
keyboard.wait['esc']

# Record events until 'esc' is pressed.
recorded = keyboard.record[until='esc']
# Then replay back at three times the speed.
keyboard.play[recorded, speed_factor=3]

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation['@@', 'my.long.email@example.com']

# Block forever, like `while True`.
keyboard.wait[]
808 để nhấp vào liên kết này.

Với biểu mẫu mới đã sẵn sàng hoạt động, vòng lặp

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
67 bên ngoài của tập lệnh có thể tiếp tục đến lần lặp tiếp theo và nhập thông tin của người tiếp theo vào biểu mẫu

Hoàn thành chương trình của bạn bằng cách thêm đoạn mã sau

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
5

Khi vòng lặp chính của

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
67 kết thúc, chương trình sẽ nhập thông tin của từng người. Trong ví dụ này, chỉ có bốn người tham gia. Nhưng nếu bạn có 4.000 người, thì việc viết một chương trình để làm việc này sẽ giúp bạn tiết kiệm rất nhiều thời gian và công sức đánh máy.

Tóm lược

Tự động hóa GUI với mô-đun

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
6 cho phép bạn tương tác với các ứng dụng trên máy tính của mình bằng cách điều khiển chuột và bàn phím. Mặc dù cách tiếp cận này đủ linh hoạt để làm bất cứ điều gì mà người dùng có thể làm, nhưng nhược điểm là các chương trình này khá mù mờ về những gì họ đang nhấp hoặc gõ. Khi viết các chương trình tự động hóa GUI, hãy cố gắng đảm bảo rằng chúng sẽ bị hỏng nhanh chóng nếu chúng được đưa ra các hướng dẫn sai. Sự cố gây khó chịu, nhưng nó tốt hơn nhiều so với việc chương trình tiếp tục bị lỗi

Bạn có thể di chuyển con trỏ chuột xung quanh màn hình và mô phỏng các lần nhấp chuột, tổ hợp phím và phím tắt bằng PyAutoGUI. Mô-đun

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
6 cũng có thể kiểm tra màu sắc trên màn hình, điều này có thể cung cấp cho chương trình tự động hóa GUI của bạn đủ ý tưởng về nội dung màn hình để biết liệu nó có bị chệch hướng hay không. Bạn thậm chí có thể cung cấp cho PyAutoGUI một ảnh chụp màn hình và để nó tìm ra tọa độ của khu vực bạn muốn nhấp vào

Bạn có thể kết hợp tất cả các tính năng PyAutoGUI này để tự động hóa mọi tác vụ lặp đi lặp lại một cách vô thức trên máy tính của mình. Trên thực tế, có thể hết sức mê hoặc khi xem con trỏ chuột tự di chuyển và thấy văn bản tự động xuất hiện trên màn hình. Tại sao không dành thời gian bạn tiết kiệm được bằng cách ngồi lại và xem chương trình của bạn làm tất cả công việc cho bạn?

câu hỏi thực hành


Q

1. Làm cách nào bạn có thể kích hoạt PyAutoGUI failsafe để dừng chương trình?

Q

2. Hàm nào trả về

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
99 hiện tại?

Q

3. Hàm nào trả về tọa độ cho vị trí hiện tại của con trỏ chuột?

Q

4. Sự khác biệt giữa

>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
05 và
>>> import pyautogui
>>> for i in range[10]:
      pyautogui.moveRel[100, 0, duration=0.25]
      pyautogui.moveRel[0, 100, duration=0.25]
      pyautogui.moveRel[-100, 0, duration=0.25]
      pyautogui.moveRel[0, -100, duration=0.25]
11 là gì?

Q

5. Những chức năng nào có thể được sử dụng để kéo chuột?

Q

6. Lệnh gọi hàm nào sẽ gõ các ký tự của

>>> pyautogui.position[]
[311, 622]
>>> pyautogui.position[]
[377, 481]
>>> pyautogui.position[]
[1536, 637]
02?

Q

7. Làm cách nào bạn có thể nhấn phím cho các phím đặc biệt, chẳng hạn như phím mũi tên trái của bàn phím?

Q

8. Làm cách nào bạn có thể lưu nội dung hiện tại của màn hình vào tệp hình ảnh có tên ảnh chụp màn hình. png ?

Q

9. Mã nào sẽ đặt khoảng dừng hai giây sau mỗi lệnh gọi hàm PyAutoGUI?

Dự án thực hành

Để thực hành, hãy viết các chương trình thực hiện các công việc sau

Đang bận

Nhiều chương trình nhắn tin tức thời xác định xem bạn đang ở chế độ chờ hay đang ở xa máy tính bằng cách phát hiện chuột không di chuyển trong một khoảng thời gian—chẳng hạn như mười phút. Có thể bạn muốn rời khỏi bàn làm việc một lúc nhưng không muốn người khác thấy trạng thái tin nhắn tức thời của bạn ở chế độ không hoạt động. Viết một kịch bản để nhích nhẹ con trỏ chuột của bạn cứ sau mười giây. Cú huých phải đủ nhỏ để nó không cản trở nếu bạn cần sử dụng máy tính của mình trong khi tập lệnh đang chạy

Bot nhắn tin tức thì

Google Talk, Skype, Yahoo Messenger, AIM và các ứng dụng nhắn tin tức thời khác thường sử dụng các giao thức độc quyền khiến người khác khó viết các mô-đun Python có thể tương tác với các chương trình này. Nhưng ngay cả những giao thức độc quyền này cũng không thể ngăn bạn viết một công cụ tự động hóa GUI

Ứng dụng Google Talk có thanh tìm kiếm cho phép bạn nhập tên người dùng vào danh sách bạn bè của mình và mở cửa sổ nhắn tin khi bạn nhấn ENTER . Tiêu điểm bàn phím tự động chuyển sang cửa sổ mới. Các ứng dụng tin nhắn tức thời khác có cách mở cửa sổ tin nhắn mới tương tự. Viết chương trình tự động gửi tin nhắn thông báo đến một nhóm người được chọn trong danh sách bạn bè của bạn. Chương trình của bạn có thể phải xử lý các trường hợp ngoại lệ, chẳng hạn như bạn bè đang ngoại tuyến, cửa sổ trò chuyện xuất hiện ở các tọa độ khác nhau trên màn hình hoặc hộp xác nhận làm gián đoạn tin nhắn của bạn. Chương trình của bạn sẽ phải chụp ảnh màn hình để hướng dẫn tương tác GUI của nó và áp dụng các cách phát hiện khi tổ hợp phím ảo của nó không được gửi.

Ghi chú

Bạn có thể muốn thiết lập một số tài khoản thử nghiệm giả mạo để không vô tình spam những người bạn thực sự của mình khi viết chương trình này

Python có thể mô phỏng thao tác nhấn phím không?

Với Pynput, chúng ta có thể yêu cầu Python mô phỏng thao tác nhấn phím , di chuyển chuột đến một điểm cụ thể trên màn hình và theo dõi đầu vào.

Làm thế nào để mô phỏng hành trình phím?

Để mô phỏng các thao tác gõ phím bằng ngôn ngữ bản địa, bạn cũng có thể sử dụng hằng số [Xnn] hoặc [Dnn] trong chuỗi được truyền cho phương thức Keys. nn specifies the virtual-key code of the key to be “pressed”. For instance, [X221]u[X221]e will “type” the u and e characters with the circumflex accent.

Chủ Đề