Skip to content

Commit db784b1

Browse files
Refactoring control methods to use control_key
1 parent 66c1df1 commit db784b1

File tree

1 file changed

+21
-90
lines changed

1 file changed

+21
-90
lines changed

botcity/core/bot.py

Lines changed: 21 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -929,10 +929,10 @@ def tab(self, wait=0, presses=1):
929929
presses (int, optional): Number of times to press the key. Defaults to 1.
930930
931931
"""
932+
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
932933
for i in range(presses):
933934
self._kb_controller.tap(Key.tab)
934-
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
935-
self.sleep(wait)
935+
self.sleep(delay)
936936

937937
def enter(self, wait=0, presses=1):
938938
"""
@@ -943,10 +943,10 @@ def enter(self, wait=0, presses=1):
943943
presses (int, optional): Number of times to press the key. Defaults to 1.
944944
945945
"""
946+
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
946947
for i in range(presses):
947948
self._kb_controller.tap(Key.enter)
948-
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
949-
self.sleep(wait)
949+
self.sleep(delay)
950950

951951
def key_right(self, wait=0):
952952
"""
@@ -1193,13 +1193,7 @@ def control_c(self, wait=0):
11931193
wait (int, optional): Wait interval (ms) after task
11941194
11951195
"""
1196-
key = Key.ctrl
1197-
if platform.system() == 'Darwin':
1198-
key = Key.cmd
1199-
with self._kb_controller.pressed(key):
1200-
self._kb_controller.tap('c')
1201-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1202-
self.sleep(delay)
1196+
self.control_key(key_to_press='c', wait=wait)
12031197
return self.get_clipboard()
12041198

12051199
def control_v(self, wait=0):
@@ -1210,13 +1204,7 @@ def control_v(self, wait=0):
12101204
wait (int, optional): Wait interval (ms) after task
12111205
12121206
"""
1213-
key = Key.ctrl
1214-
if platform.system() == 'Darwin':
1215-
key = Key.cmd
1216-
with self._kb_controller.pressed(key):
1217-
self._kb_controller.tap('v')
1218-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1219-
self.sleep(delay)
1207+
self.control_key(key_to_press='v', wait=wait)
12201208

12211209
def control_a(self, wait=0):
12221210
"""
@@ -1226,13 +1214,7 @@ def control_a(self, wait=0):
12261214
wait (int, optional): Wait interval (ms) after task
12271215
12281216
"""
1229-
key = Key.ctrl
1230-
if platform.system() == 'Darwin':
1231-
key = Key.cmd
1232-
with self._kb_controller.pressed(key):
1233-
self._kb_controller.tap('a')
1234-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1235-
self.sleep(delay)
1217+
self.control_key(key_to_press='a', wait=wait)
12361218

12371219
def control_f(self, wait=0):
12381220
"""
@@ -1242,13 +1224,7 @@ def control_f(self, wait=0):
12421224
wait (int, optional): Wait interval (ms) after task
12431225
12441226
"""
1245-
key = Key.ctrl
1246-
if platform.system() == 'Darwin':
1247-
key = Key.cmd
1248-
with self._kb_controller.pressed(key):
1249-
self._kb_controller.tap('f')
1250-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1251-
self.sleep(delay)
1227+
self.control_key(key_to_press='f', wait=wait)
12521228

12531229
def control_p(self, wait=0):
12541230
"""
@@ -1258,13 +1234,7 @@ def control_p(self, wait=0):
12581234
wait (int, optional): Wait interval (ms) after task
12591235
12601236
"""
1261-
key = Key.ctrl
1262-
if platform.system() == 'Darwin':
1263-
key = Key.cmd
1264-
with self._kb_controller.pressed(key):
1265-
self._kb_controller.tap('p')
1266-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1267-
self.sleep(delay)
1237+
self.control_key(key_to_press='p', wait=wait)
12681238

12691239
def control_u(self, wait=0):
12701240
"""
@@ -1274,13 +1244,7 @@ def control_u(self, wait=0):
12741244
wait (int, optional): Wait interval (ms) after task
12751245
12761246
"""
1277-
key = Key.ctrl
1278-
if platform.system() == 'Darwin':
1279-
key = Key.cmd
1280-
with self._kb_controller.pressed(key):
1281-
self._kb_controller.tap('u')
1282-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1283-
self.sleep(delay)
1247+
self.control_key(key_to_press='u', wait=wait)
12841248

12851249
def control_r(self, wait=0):
12861250
"""
@@ -1290,13 +1254,7 @@ def control_r(self, wait=0):
12901254
wait (int, optional): Wait interval (ms) after task
12911255
12921256
"""
1293-
key = Key.ctrl
1294-
if platform.system() == 'Darwin':
1295-
key = Key.cmd
1296-
with self._kb_controller.pressed(key):
1297-
self._kb_controller.tap('r')
1298-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1299-
self.sleep(delay)
1257+
self.control_key(key_to_press='r', wait=wait)
13001258

13011259
def control_t(self, wait=0):
13021260
"""
@@ -1306,13 +1264,7 @@ def control_t(self, wait=0):
13061264
wait (int, optional): Wait interval (ms) after task
13071265
13081266
"""
1309-
key = Key.ctrl
1310-
if platform.system() == 'Darwin':
1311-
key = Key.cmd
1312-
with self._kb_controller.pressed(key):
1313-
self._kb_controller.tap('t')
1314-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1315-
self.sleep(delay)
1267+
self.control_key(key_to_press='t', wait=wait)
13161268

13171269
def control_s(self, wait=0):
13181270
"""
@@ -1322,15 +1274,9 @@ def control_s(self, wait=0):
13221274
wait (int, optional): Wait interval (ms) after task
13231275
13241276
"""
1325-
key = Key.ctrl
1326-
if platform.system() == 'Darwin':
1327-
key = Key.cmd
1328-
with self._kb_controller.pressed(key):
1329-
self._kb_controller.tap('s')
1330-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1331-
self.sleep(delay)
1277+
self.control_key(key_to_press='s', wait=wait)
13321278

1333-
def control_key(self, key_to_press: str, wait=0):
1279+
def control_key(self, key_to_press: str | Key, wait=0):
13341280
"""
13351281
Press CTRL and one more simple key to perform a keyboard shortcut
13361282
@@ -1339,11 +1285,14 @@ def control_key(self, key_to_press: str, wait=0):
13391285
wait (int, optional): Wait interval (ms) after task.
13401286
13411287
"""
1288+
if isinstance(key_to_press, str):
1289+
key_to_press = key_to_press.lower()
1290+
13421291
key = Key.ctrl
13431292
if platform.system() == 'Darwin':
13441293
key = Key.cmd
13451294
with self._kb_controller.pressed(key):
1346-
self._kb_controller.tap(key_to_press.lower())
1295+
self._kb_controller.tap(key_to_press)
13471296
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
13481297
self.sleep(delay)
13491298

@@ -1355,13 +1304,7 @@ def control_end(self, wait=0):
13551304
wait (int, optional): Wait interval (ms) after task
13561305
13571306
"""
1358-
key = Key.ctrl
1359-
if platform.system() == 'Darwin':
1360-
key = Key.cmd
1361-
with self._kb_controller.pressed(key):
1362-
self._kb_controller.tap(Key.end)
1363-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1364-
self.sleep(delay)
1307+
self.control_key(key_to_press=Key.end, wait=wait)
13651308

13661309
def control_home(self, wait=0):
13671310
"""
@@ -1371,13 +1314,7 @@ def control_home(self, wait=0):
13711314
wait (int, optional): Wait interval (ms) after task
13721315
13731316
"""
1374-
key = Key.ctrl
1375-
if platform.system() == 'Darwin':
1376-
key = Key.cmd
1377-
with self._kb_controller.pressed(key):
1378-
self._kb_controller.tap(Key.home)
1379-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1380-
self.sleep(delay)
1317+
self.control_key(key_to_press=Key.home, wait=wait)
13811318

13821319
def control_w(self, wait=0):
13831320
"""
@@ -1387,13 +1324,7 @@ def control_w(self, wait=0):
13871324
wait (int, optional): Wait interval (ms) after task
13881325
13891326
"""
1390-
key = Key.ctrl
1391-
if platform.system() == 'Darwin':
1392-
key = Key.cmd
1393-
with self._kb_controller.pressed(key):
1394-
self._kb_controller.tap('w')
1395-
delay = max(0, wait or config.DEFAULT_SLEEP_AFTER_ACTION)
1396-
self.sleep(delay)
1327+
self.control_key(key_to_press='w', wait=wait)
13971328

13981329
def control_shift_p(self, wait=0):
13991330
"""

0 commit comments

Comments
 (0)