Browser Extensions Development (Chrome & Edge) · บทเรียน

แป้นพิมพ์ลัดด้วย API Commands

มอบทางเข้าถึงส่วนขยายของคุณอย่างรวดเร็วแก่ผู้ใช้ขั้นสูงผ่านแป้นพิมพ์ลัดทั่วระบบที่กำหนดด้วย API commands

บทเรียน 4 จาก 413 ขั้นตอน

แป้นพิมพ์ลัดด้วย API Commands เป็นบทเรียน Browser Extensions Development (Chrome & Edge) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Browser Extensions Development (Chrome & Edge) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Keyboard Shortcuts

Beyond context menus and the omnibox, the commands API lets users trigger actions with a keystroke. Shortcuts make your extension feel fast and professional to power users.

Declaring Commands

Define each command in the manifest under commands, with a suggested key combination per platform.

{
  "commands": {
    "toggle-feature": {
      "suggested_key": { "default": "Ctrl+Shift+Y" },
      "description": "Toggle the feature"
    }
  }
}

Cross-Platform Keys

macOS uses Command instead of Ctrl. Provide platform-specific suggestions so the default keys feel native everywhere.

"suggested_key": {
  "default": "Ctrl+Shift+Y",
  "mac": "Command+Shift+Y"
}

Listening for Commands

In your service worker, handle the onCommand event. The command name you declared is passed to the listener.

chrome.commands.onCommand.addListener((command) => {
  if (command === 'toggle-feature') {
    console.log('Shortcut fired')
  }
})

The Special _execute_action Command

A reserved command named _execute_action opens your popup or fires the action click. You do not handle it in code; the browser does.

{
  "commands": {
    "_execute_action": {
      "suggested_key": { "default": "Ctrl+Shift+E" }
    }
  }
}

Acting on the Current Tab

Most shortcuts operate on the active tab. Query it inside the handler, then send a message or inject a script.

chrome.commands.onCommand.addListener(async (command) => {
  const [tab] = await chrome.tabs.query({ active: true, currentWindow: true })
  chrome.tabs.sendMessage(tab.id, { command })
})

Users Can Rebind Keys

Your suggested keys are only suggestions. Users can change them on the browser's extension shortcuts page, and conflicts are resolved there.

// chrome://extensions/shortcuts lets users rebind

Limited Number of Suggestions

Browsers limit how many commands can have a suggested key (commonly four). Extra commands still work but start unbound until the user assigns a key.

Reading Registered Commands

Use commands.getAll to inspect what keys are currently bound, useful for showing a help screen in your options page.

const cmds = await chrome.commands.getAll()
cmds.forEach(c => console.log(c.name + ': ' + c.shortcut))

Avoiding Conflicts

Pick uncommon combinations to reduce clashes with the browser and other extensions. Combos with Shift plus a letter are usually safe.

Documenting Shortcuts

List your shortcuts in the options page or onboarding so users discover them. A hidden shortcut helps no one.

Quick Check

Test your commands API knowledge.

Recap

You added keyboard shortcuts:

  • Declare them under commands with per-platform keys
  • Handle onCommand in the service worker
  • Use _execute_action for the popup
  • Respect the suggested-key limit and user rebinds
  • Document shortcuts for discoverability

Shortcuts make your extension feel fast and power-user friendly.

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “แป้นพิมพ์ลัดด้วย API Commands” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “แป้นพิมพ์ลัดด้วย API Commands” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Browser Extensions Development (Chrome & Edge) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “แป้นพิมพ์ลัดด้วย API Commands”

มอบทางเข้าถึงส่วนขยายของคุณอย่างรวดเร็วแก่ผู้ใช้ขั้นสูงผ่านแป้นพิมพ์ลัดทั่วระบบที่กำหนดด้วย API commands คุณปฏิบัติ Browser Extensions Development (Chrome & Edge) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Browser Extensions Development (Chrome & Edge) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Browser Extensions Development (Chrome & Edge) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “แป้นพิมพ์ลัดด้วย API Commands” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Browser Extensions Development (Chrome & Edge) นี้ได้ไหม

ได้ บทเรียน Browser Extensions Development (Chrome & Edge) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเพิ่มรายการเมนูบริบท
  2. การผสานรวมคำสำคัญกับ Omnibox
  3. การตอบสนองต่อข้อมูลป้อนเข้า Omnibox
  4. แป้นพิมพ์ลัดด้วย API Commands
← กลับไปที่ Browser Extensions Development (Chrome & Edge)