트레이에서 마우스(여러개일때) 자동선택 > 소스정보

본문 바로가기
사이트 내 전체검색


소스정보

 

트레이에서 마우스(여러개일때) 자동선택

페이지 정보

본문

568838e1e57f5c3e786f7b8fe8f3e1ed_1745978639_8914.png
 

ㅁ 구성 파일

connect_mouse.bat — 실행 배치파일

connect_mouse.ps1 — PowerShell 스크립트

System.Windows.Forms.NotifyIcon — PowerShell 클래스

btpair.exe — BluetoothCommandLineTools 도구


1. connect_mouse.bat 내용

@echo off

powershell -ExecutionPolicy Bypass -File "connect_mouse.ps1"

pause

 

 

2. connect_mouse.ps1 내용 (전체)

# 필요한 도구 확인

$btpairPath = ".\btpair.exe"

if (!(Test-Path $btpairPath)) {

    Write-Host "btpair.exe가 현재 폴더에 없습니다. 먼저 다운로드해주세요."

    Start-Sleep -Seconds 3

    exit

}


Write-Host "`n???? 블루투스 장치 검색 중..."


# BluetoothCommandLineTools를 사용하여 페어링된 장치 목록 가져오기

$pairedDevices = & $btpairPath /list | Where-Object { $_ -match "^\[.*\]" }


# 마우스만 필터링 (이름 기준 - 필요 시 수정)

$mouseList = @()

foreach ($line in $pairedDevices) {

    if ($line -match "\[(.*?)\]\s+-\s+(.*?)$") {

        $mac = $matches[1]

        $name = $matches[2]

        if ($name -match "mouse|logitech|microsoft|razor|m720|atheris|마우스") {

            $mouseList += [PSCustomObject]@{

                Name = $name

                MAC = $mac

            }

        }

    }

}


if ($mouseList.Count -eq 0) {

    Write-Host "❌ 연결 가능한 블루투스 마우스를 찾을 수 없습니다."

    exit

}


# GUI 선택

$selected = $mouseList | Out-GridView -Title "블루투스 마우스 선택" -PassThru


if (!$selected) {

    Write-Host "⛔ 선택이 취소되었습니다."

    exit

}


# 연결 시도 전 상태 확인

Write-Host "`n???? 선택한 마우스: $($selected.Name)"

Write-Host "MAC 주소: $($selected.MAC)"

Write-Host "상태 확인 중..."


$deviceStatus = & $btpairPath /status $selected.MAC


if ($deviceStatus -match "Connected: Yes") {

    Write-Host "✅ 이미 연결되어 있습니다."

} else {

    Write-Host "???? 연결 시도 중..."

    & $btpairPath /connect $selected.MAC | Out-Host


    # 연결 후 확인

    Start-Sleep -Seconds 2

    $verifyStatus = & $btpairPath /status $selected.MAC

    if ($verifyStatus -match "Connected: Yes") {

        Write-Host "???? 연결 성공!"

    } else {

        Write-Host "⚠️ 연결 실패. 장치가 꺼져있거나 범위 밖일 수 있습니다."

    }

}

 

 

3. 추가 기능 설명

    3-1. 연결 해제 기능

btpair.exe의 /u 옵션을 사용하여 연결된 블루투스 장치의 연결을 해제할 수 있습니다.


예시: btpair.exe /u [MAC 주소]


    3-2. 자동 재연결 기능

연결이 끊어진 마우스를 자동으로 재연결하려면, 주기적으로 연결 상태를 확인하고 끊어진 경우 자동으로 연결하는 로직을 추가할 수 있습니다.


예시: btpair.exe /connect [MAC 주소]


    3-3. 트레이 아이콘에서 실행

PowerShell 스크립트에서 System.Windows.Forms.NotifyIcon 클래스를 사용하여 트레이 아이콘을 생성하고, 이를 통해 스크립트를 실행하거나 종료할 수 있습니다.


예시 코드:

Add-Type -TypeDefinition @"

using System;

using System.Windows.Forms;

public class TrayApp {

    public static void Main() {

        NotifyIcon trayIcon = new NotifyIcon();

        trayIcon.Icon = System.Drawing.SystemIcons.Information;

        trayIcon.Visible = true;

        trayIcon.Text = "블루투스 마우스 관리";

        trayIcon.ContextMenu = new ContextMenu(new MenuItem[] {

            new MenuItem("연결", (sender, e) => { /* 연결 코드 */ }),

            new MenuItem("종료", (sender, e) => { trayIcon.Visible = false; Application.Exit(); })

        });

        Application.Run();

    }

}

"@

[TrayApp]::Main()

 

 

4. 도구 안내

    4-1. 첨부파일 다운로드

    4-2. 압축을 풀고 BluetoothCLTools-1.2.0.56를 설치

    4-3. 설치 폴더에서 btpair.exe를 connect_mouse.bat, connect_mouse.ps1와 동일한 폴더에 배치합니다.

    4-4. 이후 관련 명령 실행


추천0

첨부파일

댓글목록

등록된 댓글이 없습니다.

 

회원로그인

회원가입

WIDEPOWER.COM

접속자집계

오늘
65
어제
228
최대
330
전체
8,332
유틸리티 제대로 배우기
WIDEPOWER
유틸리티 제대로 배우기

Copyright © WIDEPOWER.COM All rights reserved.